Long Running Taks in Web App/Django

Some types of web applications require to start long running tasks – like import of file, compilation etc., but  user still needs to have real time updates about progress of the task, eventually some error messages, warnings from the task (cannot import particular line, compilation error).   There are existing robust solutions like Celery, but it is aways fun to reinvent the wheel :-)   In this case we focus on simple solution, without need for request broker in middle, which enables  immediate/ real time updates on running tasks to client browser.

For our solution we will use two cool technologies/libraries web sockets and zeromq library.

Used Libraries

zeromq  (or zmq)- is C library for asynchronous messaging  with binding to many languages (python included). ZMQ has very good reputation for robustness and ease of use. In this example we used zeromq library 3.2.3, with python binding pyzmq 13.0.2, which was not yet available in latest Ubuntu distro (in there it’s still old version 2 of zmq library), so it has to be installed manually from source.

gevent-socketio – is python port of socket.io server part. Socket.io is Javascript library, that wraps web sockets and provides advanced functionality on top web socket (namespaces, custom events/messages,  heartbeat etc.). Also for web browsers, which do not support web socket, it provides alternative communication mechanism via same interface.

gevent – is requirement for gevent-socketio  – gevent is a networking library that enables non-blocking usage of sockets, for this purpose gevent is using so call green threads (greenlets)

optionally django – web application framework, it is not basically necessary – pages and socket server part can run in any other web server that support gevent (gevent itself, gunicorn, …)

jquery – Javascript library

Solution Architecture

Long running (tens of minutes, hours) tasks are usually not related to content of the web page and can be also pretty complex – so we should better keep them outside of web server in a separate process(es).   We can start processes directly from the web server, but it usually has disadvantages (like limited privileges to web server user, no control over launched processes etc.) ,so we  have another process that will launch our tasks in  separate processes. For communication between processes we will use zeromq. Additionally messages from tasks (progress, errors etc.)  -are switched to browser web socket, so client can immediately see them.   We consider linux as our OS of choice.

On following picture is general architecture of our solution:

On client side, in Javascript, we  connect socket to web server first – to receive messages from the task and display them on the page in various ways (progress message can update progress bar, error message can be displayed in some kind of log view, or raise pop up window etc.)
Then we  do Ajax call to server to start task in a separate process – web server will pass this request to “Workers Server”, which should be already running in background. Web server uses REQ-REP zmq sockets pair for this – request contains name of  function, which should  run remotely and its parameters (something like simple RPC), response confirms that process was launched, eventually any immediate errors, that prevent process to be launched.   Workers Server receives this message and launches appropriate function in a new process (using multiprocessing package).   Task itself has to open PUB zmq socket and publish relevant messages – about it’s progress, eventual errors etc.
Workers Server  contains also proxy for all PUB messages, so they are collected  from all tasks and re-published again on one well-known port – so web server can always find them in one fixed location. On the web server we define socket.io namespace, that is activated when client opens a socket  from a page and that  just passes messages from zmq SUB socket to web socket.  Here we also must subscribe only  messages related to the task started from that particular web page – this can be done by assigning unique id to our task, that is sent, when web socket is connected from page.

Python code

First let’s have some utility functions (all supporting code is in remote module – for convenience- zipped remote.py ):

init function imports right flavor of zmq –   for zmq working correctly in gevent environment we had to import zmq.green.  create_socket is just wrapping socket creation into one convenient function. zmq.LINGER assures that undelivered messages are discarded after socket is closed – otherwise zmq will try to deliver them forever (resp. until whole process is finished). context function just wraps context creation.

Next we write  “Workers Server”  code  – it is pretty simple code :

Server has basically two function – first is to receive requests for a task, which  then runs in a separate process – that is done by do_remote_call function:

This function is basically taking a function name and its arguments from received message and  using them with multiprocessing module to fork new process.   Majority of the code here is to check limit conditions and report errors . One of important limit conditions is that child processes do not grow over certain limit, another is that same call is not launched twice, if previous is still running – this is checked by supporting function _check_running:

which is using global _running dictionary.  As noted  before do_remote_call will require some function name, which is defined in current scope – for security reason we should not allow to use any name, but only functions, which are intended for this purpose – we can use this function decorator to mark that function is available for remote call:

And we indeed need some function that is called – so let’s look at one dummy function:

In this function it is important to create new zmq context (line 5),  because this code  runs in forked process and parent context there is not valid.  Also we should create a ‘pub’ socket, where we can send updates from the task (and these are relayed to client web page Javascript).

Function send_msg wraps sending message on pub socket:

As mentioned above Workers Server has also second function – to relay messages published by forked task processes – for this we can use zmq device (device is an utility that just forwards messages between input and output sockets).   In server code we had function  run_proxy for this purpose:

So that would  be all  fundamental Workers Server  pieces, now to pieces, which should fit into web server – first we have couple of utility functions in remote module:

Function sub_msg has to be used on SUB socket to subscribe to particular stream of messages (if SUB socket is not subscribed it will receive nothing).  poll_msg is used to receive message with given timeout and call_remote is used to create remote task ( opposite end of do_remote_call, which was presented above).

In the web server we will have to create a code, that responds to Ajax call and and starts a task with call_remote.  In Django we have a view (in views.py)  function for this, bound to certain url:

rjson just returns response with JSON content. JSON is used to indicate errors, which can occur when doing remote call. Also global zmq context (zmq_ctx) has to be defined somewhere –  one for all process (in our case it is in sockets.py as shown below).

We  need also server code for socket.io,  latest  python-socketio contains some code for integration with Django – we  have to put these lines to root urls.py:

This code  binds a handler to /socket.io url that can start individual namespaces  and autodiscover  looks for any sockets.py files in installed Djago applications and register namespaces defined there. Namespace is basically a ‘virtual’ socket, that has a name, and can receive and send messages limited to that namespace only.

Here is sockets.py for our application:

Note that we have to use decorator namespace to given class so it is included to socketio namespaces definition and used when client connect this namespace.   The namespace is  waiting on ‘start’ message from client and then it resends all messages from SUB socket to this namespace  socket (filtered by stream_id – an identifier of page that started the process).

And finally Javascript code using JQuery, which sits on page that initiates remote task (with two Django template tags to supply context data):

The function  launchTask   is called on button click or other appropriate event on the page.  Itl first connects to our namespace ‘/log’, then when connected it sends ‘start’ message, which  makes namespace to listen on zmq SUB socket for messages coming from task process.  When namespace  socket  is connected we  do Ajax call to start the task on server.  URL of the ajax call is directed to the view we’ve introduced earlier – and we use some generated id – stream_id – to identify both call_id (unique identifier of remote call – to prevent duplicate calls) and stream_id (to identify messages coming from our task process). When a message l come sfrom process we display it on the page ( where we have this html: <div id="result"><ul id=<messages"></ul></div> to hold messages). Also note that there are several places to report errors:

  1. When view returns HTTP error – it is handled  by error option of Ajax call
  2. When view cannot launch remote task – then Ajax returns JSON with errors key
  3. When task itself has a problem – it can return appropriate messages, which are displayed

We  also need a page to host our Javascript code (in Django it means page template + view+ url definition).

 How to Test And Run

In order to test we have to first start Workers Server as separate process.   Then we start web server – in case of Django we need to start it with gevent support –   python-socketio contains sample application “chat” containing custom command for manage.py  (I think I had to add to it gevent.monkey.patch_all()  in order to work)- we have to enable this application in Django site settings py.   Then we can start Django web server as ./manage.py runserver_socketio  and in browser open the page and click button (or whatever action that will start JavaScript above) and fun can begin.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">