Posts

Showing posts with the label python

How to make responsive GTK+ applications

Image
Introduction This weekend I've made a GTK+ application, I've done my best to make it responsive by applying my old Android development experience. Android make it clear that you should not block UI thread (main thread) not non-UI tasks like: disk IO (read a file) network IO (request remote API) internal SQLite database intensive computations  Let me quote : "You should not perform the work on the UI thread, but instead create a worker thread and do most of the work there." private class MyTask extends AsyncTask... { protected Long doInBackground(URL... urls) { // worker thread } protected void onProgressUpdate(Integer... progress) { // ui thread } protected void onPostExecute(Long result) { // ui thread } } GTK+ GTK+ is not threadsafe, in the sense all calls to GTK+ should be from a single thread that is the main thread or the UI thread, which seems similar to Android. We have a class that loads the glade XM...

Boosting performance and concurrency in Python

Image
Python provides a base socket server that got no concurrency support by default, which can be used to create any server including HTTPServer or WSGI applications servers like the wsgiref. You can plugin concurrency support using ThreadingMixIn or ForkingMixIn   this would allow our pure-python server to handle multiple requests by forking another process or starting a new thread while the main thread in the main process keeps accepting requests. In this post I'm going to introduce my own PooledProcessMixIn and its features over other solutions. The concept of Pool BSD mascot with a fork I've taken a look at the code of those Mix-Ins and found serious performance issue with it as they allocate a new process or new thread each time a request comes to the server. Beside delaying the response waiting for the allocation, it's an open-ended approach (no re-using of those threads or processes). The pool approach is to allocate a number of threads or fork a number of pr...