Replies: 14 comments
-
You can't use threading and queues with eventlet, they are incompatible. See https://eventlet.net/doc/patching.html for how to monkey patch them to become compatible. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your help. I've tried to use eventlet.monkey_patch(thread=True)
import threading But it will not return from the threading. So after the |
Beta Was this translation helpful? Give feedback.
-
@Abbyyan I really don't know how to help, this isn't a problem with this package, you need to work around the limitations eventlet imposes in your application. Monkey patching helps in most cases, but not always. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot. According to https://python-socketio.readthedocs.io/en/latest/, I've changed to use from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app = socketio.WSGIApp(sio)
pywsgi.WSGIServer(('', 8000), app,
handler_class=WebSocketHandler).serve_forever() It can send message from server to client even in a sub thread now. But it's wired that it will take a long time receiving message from server. The log is as follows. After the server % python server.py
Server initialized for gevent.
59b2423ec2a7474dba22a5b789de6666: Sending packet OPEN data {'sid': '59b2423ec2a7474dba22a5b789de6666', 'upgrades': ['websocket'], 'pingTimeout': 60000, 'pingInterval': 25000}
new client connect 59b2423ec2a7474dba22a5b789de6666
59b2423ec2a7474dba22a5b789de6666: Sending packet MESSAGE data 0
59b2423ec2a7474dba22a5b789de6666: Received request to upgrade to websocket
59b2423ec2a7474dba22a5b789de6666: Upgrade to websocket successful
59b2423ec2a7474dba22a5b789de6666: Received packet PING data None
59b2423ec2a7474dba22a5b789de6666: Sending packet PONG data None
59b2423ec2a7474dba22a5b789de6666: Received packet MESSAGE data 2["response","id: 0"]
received event "response" from 59b2423ec2a7474dba22a5b789de6666 [/]
59b2423ec2a7474dba22a5b789de6666: Received packet MESSAGE data 2["response","id: 1"]
received event "response" from 59b2423ec2a7474dba22a5b789de6666 [/]
(2) id: 0
(2) id: 1
data here: id: 0
emitting event "client_response" to all [/]
59b2423ec2a7474dba22a5b789de6666: Sending packet MESSAGE data 2["client_response","data send from server id: 0"]
(3)data send from server id: 0
data here: id: 1
emitting event "client_response" to all [/]
59b2423ec2a7474dba22a5b789de6666: Sending packet MESSAGE data 2["client_response","data send from server id: 1"]
(3)data send from server id: 1 # it will take a long time after this log
59b2423ec2a7474dba22a5b789de6666: Received packet PING data None
59b2423ec2a7474dba22a5b789de6666: Sending packet PONG data None Only after the server print the last two lines log, the client side can receive the message send from server. My expectation is the 59b2423ec2a7474dba22a5b789de6666: Received packet PING data None
59b2423ec2a7474dba22a5b789de6666: Sending packet PONG data None |
Beta Was this translation helpful? Give feedback.
-
Gevent is exactly the same as eventlet with regards to blocking and patching. As I said a few times already, you need to learn how to work with these platforms. You are not using standard Python anymore, these are asynchronous frameworks. The rules are different. |
Beta Was this translation helpful? Give feedback.
-
It seems like the |
Beta Was this translation helpful? Give feedback.
-
@Abbyyan without monkey patching it will not work. With monkey-patching I believe eventlet still has some issues/bugs that haven't been solved in the implementation of multiprocessing. |
Beta Was this translation helpful? Give feedback.
-
I ran into the same issue. My application is interacting with threading/queue dependent hardware. It works well with http polling |
Beta Was this translation helpful? Give feedback.
-
You can't use greenlet frameworks with code that hasn't been written specifically for them. Monkey patching replaces a big portion of the standard library with compatible code. If you have other code that is blocking you have to fix it so that it is greenlet friendly and does not block. If you rely on third party libraries that is unlikely to have a solution unless you find another library that supports greenlets or write one yourself. |
Beta Was this translation helpful? Give feedback.
-
I see, I guess I would have to run my blocking code in a different process feeding a multiprocessing message queue. On the concurrent side of things (socketio application) I would have to run a background task |
Beta Was this translation helpful? Give feedback.
-
@stansotn not a terrible idea, this is actually a good compromise solution. I'm not 100% sure multiprocessing can be used under eventlet or gevent though, you may want to confirm that before you spend any significant amount of time on this. |
Beta Was this translation helpful? Give feedback.
-
Confirming that multiprocessing works well with socketio and eventlet when monkeypatched. I wonder why does it need monkeypatching at all.. I am able to run all my cpu bound code in the child process and communicate via multiprocessing queues. |
Beta Was this translation helpful? Give feedback.
-
To be able to communicate with the child process without blocking. |
Beta Was this translation helpful? Give feedback.
-
Right, I made sure all calls to the multiprocessing queue are non-blocking, now everything runs without monkeypatching! # A non-blocking multiprocessing queue get example.
m = None
try:
m = tx_queue.get(False)
except queue.Empty:
pass
return m ps: I noticed monkeypatching breaks the ability to generate coverage. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to do as following.
The
server.py
is as follows:The
client.py
is as follows:The server can get the message send from client, and can process data. But after the server do
sio.emit('client_response', data)
in the Thread, the client can't receive the processed data from server (If i do theemit
in the main thread, it works well ). Could you please give me some suggestions about this please? Thanks a lot.The log is as follows:
On server side:
On client side:
Beta Was this translation helpful? Give feedback.
All reactions