Question

I have an already written large app using standard python threading constructs such as threads, queues, normal sockets, and multiprocessing. It has a web frontend implemented using Flask.

I want to expose a certain part of the apps state in real time using websockets. I looked into Flask-Sockets which uses gevent and gunicorn.

Does my whole app have to use the event-driven model, or can I leave the rest of the blocking code the way it is? (Basic tests seem to having both blocking and evented code, but are there any caveats and will I have to rewrite a large portion of the code?)

I haven't found the answer Googling and checking out the gevent and gunicorn homepages and FAQs. I ask because I know that gevent can be implemented by monkey patching existing libraries.

Was it helpful?

Solution

It is not recommended. The docs say nothing about thread safety guarantees between stuff done on greenlets and stuff done on normal threads. It does not appear that this behavior is supported and there is not much online when you search for problems you will run into.

You will see weird behavior and errors in the interaction between the two.

For example if you put something into a gevent.Queue from a normal thread it may take many seconds for the gevent thread to notice something is in there.

OTHER TIPS

You can selectively monkey patch as seen here. That might work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top