Flask-SocketIO: AttributeError: module 'select' has no attribute 'poll'

I am trying to migrate from Flask-SocketIO==3.3.2 to the newest version 4.0.

I am using python rq for background tasks…the following code crashes when trying to emit a msg to the client:

from flask_socketio import SocketIO
import json


def clientID_json_generator(hid):
    socketio = SocketIO(message_queue='redis://')

    with open('cfg/static/elephant_client_version.json') as EcVersionFile:
        EcVersion = json.load(EcVersionFile)

    if hid["elephant_client"] != EcVersion:
        print(hid["elephant_client"])
        print(EcVersion)
        socketio.emit('client_outdated', EcVersion)

    with open('cfg/session/clientidentification.json', 'w') as outfile:
        json.dump(hid, outfile, indent=4)
    socketio.emit('status', {'code': 202, 'message': 'client ID json generation finished!'})

the error msg:

20:15:00 AttributeError: module 'select' has no attribute 'poll'
Traceback (most recent call last):
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/worker.py", line 812, in perform_job
    rv = job.perform()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/job.py", line 588, in perform
    self._result = self._execute()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/job.py", line 594, in _execute
    return self.func(*self.args, **self.kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/app/elephant/clientidentification.py", line 18, in clientID_json_generator
    socketio.emit('status', {'code': 202, 'message': 'client ID json generation finished!'})
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/flask_socketio/__init__.py", line 400, in emit
    skip_sid=skip_sid, callback=callback, **kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/server.py", line 278, in emit
    skip_sid=skip_sid, callback=callback, **kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/pubsub_manager.py", line 68, in emit
    'host_id': self.host_id})
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/redis_manager.py", line 76, in _publish
    return self.redis.publish(self.channel, pickle.dumps(data))
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/client.py", line 2716, in publish
    return self.execute_command('PUBLISH', channel, message)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/client.py", line 772, in execute_command
    connection = pool.get_connection(command_name, **options)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/connection.py", line 994, in get_connection
    connection.connect()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/connection.py", line 500, in connect
    self._selector = DefaultSelector(sock)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/selector.py", line 196, in DefaultSelector
    return _DEFAULT_SELECTOR(sock)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/selector.py", line 123, in __init__
    self.read_poller = select.poll()
AttributeError: module 'select' has no attribute 'poll'
Traceback (most recent call last):
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/worker.py", line 812, in perform_job
    rv = job.perform()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/job.py", line 588, in perform
    self._result = self._execute()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/rq/job.py", line 594, in _execute
    return self.func(*self.args, **self.kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/app/elephant/clientidentification.py", line 18, in clientID_json_generator
    socketio.emit('status', {'code': 202, 'message': 'client ID json generation finished!'})
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/flask_socketio/__init__.py", line 400, in emit
    skip_sid=skip_sid, callback=callback, **kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/server.py", line 278, in emit
    skip_sid=skip_sid, callback=callback, **kwargs)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/pubsub_manager.py", line 68, in emit
    'host_id': self.host_id})
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/socketio/redis_manager.py", line 76, in _publish
    return self.redis.publish(self.channel, pickle.dumps(data))
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/client.py", line 2716, in publish
    return self.execute_command('PUBLISH', channel, message)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/client.py", line 772, in execute_command
    connection = pool.get_connection(command_name, **options)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/connection.py", line 994, in get_connection
    connection.connect()
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/connection.py", line 500, in connect
    self._selector = DefaultSelector(sock)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/selector.py", line 196, in DefaultSelector
    return _DEFAULT_SELECTOR(sock)
  File "/Users/meow/data/phd/phd_projects/pipeline/code/elephant_server/venv/lib/python3.6/site-packages/redis/selector.py", line 123, in __init__
    self.read_poller = select.poll()
AttributeError: module 'select' has no attribute 'poll'

What am I doing wrong? The code works just fine with the old version.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (11 by maintainers)

Most upvoted comments

@meowlz you said that you are monkey patching early, but really the facts contradict what you are saying. Clearly there must be some side effect of switching the RQ version that I believe must be affecting when the monkey patching is done.

I have seen this problem in the past. The Redis client checks if there is a select.poll function available, and if it is, then it enables some code that uses it. This choice is made here. Eventlet monkey patches the select module and as part of that removes the poll function, since it does not provide an implementation of it for greenlets. Your problem is that the select module is not monkey patched at the time redis is imported, and that makes it enable the code that uses select.poll. A bit later the monkey patching happens, and poll goes away, so the first time it is used you get this error.