only monkey patch required modules/functions (fix errors such as 'module' object has no attribute 'epoll')
See original GitHub issueI am working on a project that uses Flask-SocketIO and runs the select.epoll()
function in a websocket callback. When debug=False
, there is no issue and everything works using both eventlet
and gevent
. Also, when debug=True
and mode=eventlet
there is no issue.
However, when debug=True
and mode=gevent
I get the exception (at the bottom) from the following psuedo code. I’m not sure if this is a Flask-SocketIO issue or a gevent issue, so feel free to redirect me if I’m in the wrong place.
from flask import Flask
from flask_socketio import SocketIO
import select
app = Flask('name')
socketio = SocketIO(async_mode='gevent')
socketio.run(app, debug=True)
@socketio.on('connect')
def client_connected():
select.epoll()
The actual code in question is here for the flask code and here for the select.epoll()
call.
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/gevent/pywsgi.py", line 936, in handle_one_response
self.run_application()
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/gevent/pywsgi.py", line 909, in run_application
self.result = self.application(self.environ, self.start_response)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/flask_socketio/__init__.py", line 42, in __call__
start_response)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/engineio/middleware.py", line 47, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/socketio/server.py", line 353, in handle_request
return self.eio.handle_request(environ, start_response)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/engineio/server.py", line 278, in handle_request
socket.handle_post_request(environ)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/engineio/socket.py", line 103, in handle_post_request
self.receive(pkt)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/engineio/socket.py", line 55, in receive
async=self.server.async_handlers)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/engineio/server.py", line 378, in _trigger_event
return self.handlers[event](*args)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/socketio/server.py", line 509, in _handle_eio_message
self._handle_connect(sid, pkt.namespace)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/socketio/server.py", line 414, in _handle_connect
self.environ[sid]) is False:
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/socketio/server.py", line 481, in _trigger_event
return self.handlers[namespace][event](*args)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/flask_socketio/__init__.py", line 224, in _handler
*args)
File "/home/.virtualenvs/p3/lib/python3.4/site-packages/flask_socketio/__init__.py", line 589, in _handle_event
ret = handler()
File "gdbgui/backend.py", line 64, in client_connected
_gdb[request.sid] = GdbController(gdb_path=GDB_PATH, gdb_args=['-nx', '--interpreter=mi2'])
File "/home/git/pygdbmi/pygdbmi/gdbcontroller.py", line 33, in __init__
self.epoll = select.epoll()
AttributeError: 'module' object has no attribute 'epoll'
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
only monkey patch required modules/functions (fix errors such ...
I am working on a project that uses Flask-SocketIO and runs the select.epoll() function in a websocket callback. When debug=False, there is ...
Read more >gevent - 'module' object has no attribute 'epoll' with Python2-7 ...
I am getting the following error (AttributeError: 'module' object has no attribute 'epoll') I believe this started happening only recently.
Read more >Monkey Patching Python Code - MachineLearningMastery.com
In this tutorial, we are going to see how we can apply this technique to some Keras and TensorFlow code. After finishing this...
Read more >Source code for gevent.monkey
For more complex patching, gevent provides a helper method that you can call to replace attributes of modules with attributes of your own...
Read more >Chapter08 Monkeypatching | Pragmatic AI Labs and Solutions
Patching is needed when there is no way to pass an object (with the desired behavior) to the test. Sometimes, when writing code,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My understanding is that
select.epoll()
is not a greenlet friendly function at this time. Neither gevent nor eventlet implement it. In the cases that you don’t get the crash, you are simply using Python’s original epoll function, which is blocking, so it won’t allow other greenlets in the system to run concurrently.The case in which you get the error is caused by the monkey patching of gevent. In that case, gevent replaces the
select
package with its own greenlet aware version, and the replacement lacks the epoll function.Fixed in https://github.com/miguelgrinberg/Flask-SocketIO/commit/55d02d97708bd91b7d8f761ab57aba8d946039ff.