Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have many ways to single-thread this kind of thing.</p> <p>One standard approach is <strong>Update First</strong>. You do an update which will seize an exclusive lock on the row; then do your work; and finally commit the change. For this to work, you need to bypass the ORM's caching. </p> <p>Another standard approach is to have a separate, single-threaded application server that isolates the Web transactions from the complex calculation. </p> <ul> <li><p>Your web application can create a queue of scoring requests, spawn a separate process, and then write the scoring requests to this queue. The spawn can be put in Django's <code>urls.py</code> so it happens on web-app startup. Or it can be put into separate <code>manage.py</code> admin script. Or it can be done "as needed" when the first scoring request is attempted.</p></li> <li><p>You can also create a separate WSGI-flavored web server using Werkzeug which accepts WS requests via urllib2. If you have a single port number for this server, requests are queued by TCP/IP. If your WSGI handler has one thread, then, you've achieved serialized single-threading. This is slightly more scalable, since the scoring engine is a WS request and can be run anywhere.</p></li> </ul> <p>Yet another approach is to have some other resource that has to be acquired and held to do the calculation. </p> <ul> <li><p>A Singleton object in the database. A single row in a unique table can be updated with a session ID to seize control; update with session ID of <code>None</code> to release control. The essential update has to include a <code>WHERE SESSION_ID IS NONE</code> filter to assure that the update fails when the lock is held by someone else. This is interesting because it's inherently race-free -- it's a single update -- not a SELECT-UPDATE sequence.</p></li> <li><p>A garden-variety semaphore can be used outside the database. Queues (generally) are easier to work with than a low-level semaphore.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload