10

At work I'm not allowed to use perl for web services. Python is allowed however.

What I need to do is serve up the results of some very slow c++ binaries. Each exe takes up to 20 seconds to run. In perl I'd just use mojolicious's non blocking event loop ( an example.of which is given here. http://blogs.perl.org/users/joel_berger/2014/01/writing-non-blocking-applications-with-mojolicious-part-3.html )

How would one go about doing this with django and python?

tlrrd
  • 322
  • 2
  • 8
  • 1
    Django is more about web-apps, not servers. You can use the built-in python module [`socket`](http://docs.python.org/2/library/socket.html), which is very simple and low-level, or any of the reactor frameworks listed in the answers below (which are usually more powerful, but might be overkill, depending on what you're looking for) – yuvi Jan 26 '14 at 11:49
  • 1
    What you really need is a port of [AnyEvent::Util](http://metacpan.org/module/AnyEvent::Util)'s fork_call (as I have been building myself with [Mojo::IOLoop::ForkCall](http://metacpan.org/module/Mojo::IOLoop::ForkCall)) and integrate it into some python event loop. Then again, you could talk your `$work` into letting you use the right tool for the job :-) – Joel Berger Feb 01 '14 at 22:32

3 Answers3

6

Tornado using non blocking IO , the concepts are the same as in perl or node js event loop, multiple tasks per thread and so on.

Slow Harry
  • 1,777
  • 3
  • 21
  • 40
4

Probably won't be possible with Django, as the entire framework will need to be built specifically for running inside an event loop. In an event-driven framework, slow operations (I/O for example) needs to be implemented using callbacks, so that the actual I/O can be offloaded to the event loop itself, and the callback only called when the operation has finished; Django is not implemented like this.

Take a look at Twisted — it is an event-driven networking engine for Python that also has some web application frameworks built on top of it.

lanzz
  • 38,081
  • 8
  • 81
  • 91
3

Take a look at A clean, lightweight alternative to Python's twisted. I'd choose gevent for a web app, as it runs with uWSGI--the most versatile web server to run Python code.

Community
  • 1
  • 1
jwalker
  • 1,939
  • 15
  • 27