Python-socketio: Error while running the server example from the docs

Created on 19 Nov 2020  路  13Comments  路  Source: miguelgrinberg/python-socketio

I'm trying to run this example which is in the docs here

import eventlet
import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})

@sio.event
def connect(sid, environ):
    print('connect ', sid)

@sio.event
def my_message(sid, data):
    print('message ', data)

@sio.event
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)

But as soon as I execute it I get this error

(15262) wsgi starting up on http://0.0.0.0:5000
(15262) wsgi exited, is_accepting=True
Traceback (most recent call last):
  File "the_path_in_my_pc/__init__.py", line 70, in <module>
    eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/wsgi.py", line 990, in server
    client_socket, client_addr = sock.accept()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/greenio/base.py", line 230, in accept
    self._trampoline(fd, read=True, timeout=self.gettimeout(), timeout_exc=_timeout_exc)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/greenio/base.py", line 208, in _trampoline
    return trampoline(fd, read=read, write=write, timeout=timeout,
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/hubs/__init__.py", line 155, in trampoline
    listener = hub.add(hub.READ, fileno, current.switch, current.throw, mark_as_closed)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/hubs/kqueue.py", line 53, in add
    self._control([event], 0, 0)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/eventlet/hubs/kqueue.py", line 39, in _control
    return self.kqueue.control(events, max_events, timeout)
TypeError: changelist must be an iterable of select.kevent objects

What may be the cause?

question

Most helpful comment

@miguelgrinberg only at macos with python3.9

All 13 comments

I don't know yet. I also noticed it in my unit tests. It happens only on Python 3.9 on Mac, and I do not believe it is related to this package, but I need to investigate and see if others are seeing this error as well.

Ok, for the moment I'll use python 3.7 then.

I don't know yet. I also noticed it in my unit tests. It happens only on Python 3.9 on Mac, and I do not believe it is related to this package, but I need to investigate and see if others are seeing this error as well.

same error. only for macos python3.9

import asyncio
from flask_socketio import SocketIO


async def main():
    await asyncio.sleep(1)

if __name__ == '__main__':
    asyncio.run(main())

error:

Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 261, in _add_reader
    key = self._selector.get_key(fd)
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/selectors.py", line 193, in get_key
    raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '4 is not registered'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/zhang/github/leadscloud/leads_api/test2.py", line 10, in <module>
    asyncio.run(main())
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 39, in run
    loop = events.new_event_loop()
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 761, in new_event_loop
    return get_event_loop_policy().new_event_loop()
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/events.py", line 659, in new_event_loop
    return self._loop_factory()
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 54, in __init__
    super().__init__(selector)
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 61, in __init__
    self._make_self_pipe()
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 112, in _make_self_pipe
    self._add_reader(self._ssock.fileno(), self._read_from_self)
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/selector_events.py", line 263, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/selectors.py", line 523, in register
    self._selector.control([kev], 0, 0)
TypeError: changelist must be an iterable of select.kevent objects
Exception ignored in: <function BaseEventLoop.__del__ at 0x1059d7c10>
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 683, in __del__
    self.close()
  File "/usr/local/Cellar/[email protected]/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/unix_events.py", line 63, in close
    if self._signal_handlers:
AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers'
sys:1: RuntimeWarning: coroutine 'main' was never awaited

@leadscloud your code runs without errors for me.

@miguelgrinberg only at macos with python3.9

@leadscloud any particular 3.9? I have 3.9.0 here on my Mac and still don't get any errors.

FWIW a similar error has come up running the python-engineio tests under NixOS with python 3.9 on Mac. It's fine on other platforms and with python 3.8 on Mac.

The inital error appears to be a KeyError, but during handling another KeyError and then the TypeError reported by the OP occur.

=================================== FAILURES ===================================
_______________________ AsgiTests.test_engineio_routing ________________________

self = <_UnixSelectorEventLoop running=False closed=False debug=False>, fd = 12
callback = <bound method BaseSelectorEventLoop._read_from_self of <_UnixSelectorEventLoop running=False closed=False debug=False>>
args = (), handle = <Handle BaseSelectorEventLoop._read_from_self()>

    def _add_reader(self, fd, callback, *args):
        self._check_closed()
        handle = events.Handle(callback, args, self, None)
        try:
>           key = self._selector.get_key(fd)

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:261: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <selectors.KqueueSelector object at 0x109ab8c10>, fileobj = 12

    def get_key(self, fileobj):
        """Return the key associated to a registered file object.

        Returns:
        SelectorKey for this file object
        """
        mapping = self.get_map()
        if mapping is None:
            raise RuntimeError('Selector is closed')
        try:
            return mapping[fileobj]
        except KeyError:
>           raise KeyError("{!r} is not registered".format(fileobj)) from None
E           KeyError: '12 is not registered'

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/selectors.py:193: KeyError

During handling of the above exception, another exception occurred:

self = <tests.asyncio.test_async_asgi.AsgiTests testMethod=test_engineio_routing>

    def test_engineio_routing(self):
        mock_server = mock.MagicMock()
        mock_server.handle_request = AsyncMock()
        app = async_asgi.ASGIApp(mock_server)
        scope = {'type': 'http', 'path': '/engine.io/'}
>       _run(app(scope, 'receive', 'send'))

tests/asyncio/test_async_asgi.py:52: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/asyncio/test_async_asgi.py:30: in _run
    return asyncio.get_event_loop().run_until_complete(coro)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/events.py:639: in get_event_loop
    self.set_event_loop(self.new_event_loop())
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/events.py:659: in new_event_loop
    return self._loop_factory()
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/unix_events.py:54: in __init__
    super().__init__(selector)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:61: in __init__
    self._make_self_pipe()
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:112: in _make_self_pipe
    self._add_reader(self._ssock.fileno(), self._read_from_self)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:263: in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <selectors.KqueueSelector object at 0x109ab8c10>, fileobj = 12
events = 1, data = (<Handle BaseSelectorEventLoop._read_from_self()>, None)

    def register(self, fileobj, events, data=None):
        key = super().register(fileobj, events, data)
        try:
            if events & EVENT_READ:
                kev = select.kevent(key.fd, select.KQ_FILTER_READ,
                                    select.KQ_EV_ADD)
>               self._selector.control([kev], 0, 0)
E               TypeError: changelist must be an iterable of select.kevent objects

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/selectors.py:523: TypeError
_______________________ AsgiTests.test_lifespan_invalid ________________________

self = <_UnixSelectorEventLoop running=False closed=False debug=False>, fd = 15
callback = <bound method BaseSelectorEventLoop._read_from_self of <_UnixSelectorEventLoop running=False closed=False debug=False>>
args = (), handle = <Handle BaseSelectorEventLoop._read_from_self()>

    def _add_reader(self, fd, callback, *args):
        self._check_closed()
        handle = events.Handle(callback, args, self, None)
        try:
>           key = self._selector.get_key(fd)

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:261: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <selectors.KqueueSelector object at 0x109974bb0>, fileobj = 15

    def get_key(self, fileobj):
        """Return the key associated to a registered file object.

        Returns:
        SelectorKey for this file object
        """
        mapping = self.get_map()
        if mapping is None:
            raise RuntimeError('Selector is closed')
        try:
            return mapping[fileobj]
        except KeyError:
>           raise KeyError("{!r} is not registered".format(fileobj)) from None
E           KeyError: '15 is not registered'

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/selectors.py:193: KeyError

During handling of the above exception, another exception occurred:

self = <tests.asyncio.test_async_asgi.AsgiTests testMethod=test_lifespan_invalid>

    def test_lifespan_invalid(self):
        app = async_asgi.ASGIApp('eio')
        scope = {'type': 'lifespan'}
        receive = AsyncMock(side_effect=[{'type': 'lifespan.foo'},
                                         {'type': 'lifespan.shutdown'}])
        send = AsyncMock()
>       _run(app(scope, receive, send))

tests/asyncio/test_async_asgi.py:250: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/asyncio/test_async_asgi.py:30: in _run
    return asyncio.get_event_loop().run_until_complete(coro)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/events.py:639: in get_event_loop
    self.set_event_loop(self.new_event_loop())
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/events.py:659: in new_event_loop
    return self._loop_factory()
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/unix_events.py:54: in __init__
    super().__init__(selector)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:61: in __init__
    self._make_self_pipe()
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:112: in _make_self_pipe
    self._add_reader(self._ssock.fileno(), self._read_from_self)
/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/asyncio/selector_events.py:263: in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <selectors.KqueueSelector object at 0x109974bb0>, fileobj = 15
events = 1, data = (<Handle BaseSelectorEventLoop._read_from_self()>, None)

    def register(self, fileobj, events, data=None):
        key = super().register(fileobj, events, data)
        try:
            if events & EVENT_READ:
                kev = select.kevent(key.fd, select.KQ_FILTER_READ,
                                    select.KQ_EV_ADD)
>               self._selector.control([kev], 0, 0)
E               TypeError: changelist must be an iterable of select.kevent objects

/nix/store/8b9sqwfvscwscq28lnf1gbiivncpyhgx-python3-3.9.4/lib/python3.9/selectors.py:523: TypeError

See https://hydra.nixos.org/build/142505307/nixlog/2 for the full output (which is polluted by a large number of AttributeError: '_UnixSelectorEventLoop' object has no attribute '_signal_handlers' errors which appear not to be fatal).

@graham33 I cannot reproduce this, and those _signal_handlers errors in your log are very concerning. Unless you figure out why those occur and you address them I can't really consider this a problem on this side, your asyncio installation seems broken.

This appears to be a bug in eventlet that only occurs with recent Python 3.9.x versions (did not happen for me on 3.9.0, but it does on 3.9.5). See https://github.com/eventlet/eventlet/issues/670.

Since there is nothing I can do to help with this issue I'll close. Hopefully Eventlet will have a fix soon.

Hello @albertoxamin
as @Miguelgrinberg says

this error only appears on Mac with the version 3.9.x of python

I offer you some possible solutions

  1. Replace python3.9.x (3.9.1, 3.9.2 etc ...) by python3.8.x or later

  2. If you use DJANGO this error only happens when you execute the command "./manage.py makemigrations and migrate" on Mac only, but it is not a good idea to replace the recent version of python for an old version

if you use django, i offer you a simple solution:

"Comment in your code any line that uses socketio"

here is the example in my case

In my socketio.py

# import socketio

# class YourSocketIOClassNamespace(socketio.Namespace):

#     def on_connect(self, sid, environ):
#         print("Connected >>>>>>>>> : ", sid)

Also command wherever a socket instance is used and run the command for migration ('makemigration and migrate')
and after that remove the comments wherever you just put it

and finaly run your server: it works

Hello @albertoxamin

as @Miguelgrinberg says

this error only appears on Mac with the version 3.9.x of python

I offer you some possible solutions


  1. Replace python3.9.x (3.9.1, 3.9.2 etc ...) by python3.8.x or later

  2. If you use DJANGO this error only happens when you execute the command "./manage.py makemigrations and migrate" on Mac only, but it is not a good idea to replace the recent version of python for an old version

if you use django, i offer you a simple solution:


"Comment in your code any line that uses socketio"

here is the example in my case

In my socketio.py


# import socketio



# class YourSocketIOClassNamespace(socketio.Namespace):



#     def on_connect(self, sid, environ):

#         print("Connected >>>>>>>>> : ", sid)

Also command wherever a socket instance is used and run the command for migration ('makemigration and migrate')

and after that remove the comments wherever you just put it

and finaly run your server: it works

Well I solved it months ago by using python 3.7 instead

Hello @albertoxamin
as @miguelgrinberg says
this error only appears on Mac with the version 3.9.x of python
I offer you some possible solutions

  1. Replace python3.9.x (3.9.1, 3.9.2 etc ...) by python3.8.x or later
  2. If you use DJANGO this error only happens when you execute the command "./manage.py makemigrations and migrate" on Mac only, but it is not a good idea to replace the recent version of python for an old version

if you use django, i offer you a simple solution:

"Comment in your code any line that uses socketio"
here is the example in my case
In my socketio.py

# import socketio



# class YourSocketIOClassNamespace(socketio.Namespace):



#     def on_connect(self, sid, environ):

#         print("Connected >>>>>>>>> : ", sid)

Also command wherever a socket instance is used and run the command for migration ('makemigration and migrate')
and after that remove the comments wherever you just put it
and finaly run your server: it works

Well I solved it months ago by using python 3.7 instead

ok I see

Was this page helpful?
0 / 5 - 0 ratings