How to create a Python service that keeps running after answering?

Asked

Viewed 960 times

3

I need to do long-pooling on a "Main" site to receive events. Then I process and filter these events and I have clients who do long-pooling on me. I need to keep this in memory, not persist, and provide it to my clients. Whether there are no customers or have many customers pooling, I always need to do a single pooling on the "main" site. I intend to host this in an Apache Httpd, even if some specific module is required.

  • Usually if you do this kind of thing with Apache, the user will be occupying a thread precious of the server during all the time that runs the script, and with few requests httpd service will be completely depleted, preventing access by other users.

1 answer

2

I find it extremely unlikely that you will be able to do such a service using Apache. I don’t know how mod_python works, but if it’s like any HTTP API, it’s request-based, and you have little or no control over the execution of your Python context, so Apache can thread, kill and duplicate processes at your beck and callpleasure, and in each of them instantiate a different Python context. The part you would have control of is the part that handles a single request, coming from your client.

I suggest you do a "standalone" process, run alone, and that it itself be the HTTP server. This is very easy to do with the library gevent, for example (although it does not have very good support for Python 3, the version for Python 3 is not official).

With the very gevent you can do asynchronous HTTP connection on other servers (gevent.httplib), or you can use this library here for more advanced features.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.