Mix Python Twisted with multiprocessing?

前端 未结 3 1873
半阙折子戏
半阙折子戏 2020-12-03 11:37

I need to write a proxy like program in Python, the work flow is very similar to a web proxy. The program sits in between the client and the server, incept requests sent by

相关标签:
3条回答
  • 2020-12-03 12:09

    ampoule is the first thing I think when reading your question.

    It is a simple process pool implementation which uses the AMP protocol to communicate. You can use the deferToAMPProcess function, it's very easy to use.

    0 讨论(0)
  • Twisted has its own event-driven way of running subprocesses which is (in my humble, but correct, opinion) better than the multiprocessing module. The core API is spawnProcess, but tools like ampoule provide higher-level wrappers over it.

    If you use spawnProcess, you will be able to handle output from subprocesses in the same way you'd handle any other event in Twisted; if you use multiprocessing, you'll need to develop your own queue-based way of getting output from a subprocess into the Twisted mainloop somehow, since the normal callFromThread API that a thread might use won't work from another process. Depending on how you call it, it will either try to pickle the reactor, or just use a different non-working reactor in the subprocess; either way it will lose your call forever.

    0 讨论(0)
  • 2020-12-03 12:24

    You can try something like Cooperative Multitasking technique as it's described there http://us.pycon.org/2010/conference/schedule/event/73/ . It's simillar to technique as Glyph menitioned and it's worth a try.

    You can try to use ZeroMQ with Twisted but it's really hard and experimental for now :)

    0 讨论(0)
提交回复
热议问题