python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.23k stars 518 forks source link

Python 3.10 support (loop parameter was removed from Lock()) #916

Closed SergeiMinaev closed 3 years ago

SergeiMinaev commented 3 years ago

Hello. I'm trying python 3.10 and it seems like websockets doesn't support it yet.

How to reproduce: Get a working websockets server and try to run a client with python3.10:

import websockets

async def hello():
    uri = "ws://localhost:8100"
    async with websockets.connect(uri) as websocket:
        await websocket.send('test')
asyncio.get_event_loop().run_until_complete(hello())

This code breaks with such error:

Traceback (most recent call last):
  File "ws_cl.py", line 8, in <module>
    asyncio.get_event_loop().run_until_complete(hello())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "ws_cl.py", line 6, in hello
    async with websockets.connect(uri) as websocket:
  File "venv/lib/python3.10/site-packages/websockets/client.py", line 517, in __aenter__
    return await self
  File "venv/lib/python3.10/site-packages/websockets/client.py", line 535, in __await_impl__
    transport, protocol = await self._create_connection()
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1080, in create_connection
    transport, protocol = await self._create_connection_transport(
  File "/usr/lib/python3.10/asyncio/base_events.py", line 1098, in _create_connection_transport
    protocol = protocol_factory()
  File "venv/lib/python3.10/site-packages/websockets/client.py", line 69, in __init__
    super().__init__(**kwargs)
  File "venv/lib/python3.10/site-packages/websockets/protocol.py", line 235, in __init__
    self._drain_lock = asyncio.Lock(
  File "/usr/lib/python3.10/asyncio/locks.py", line 77, in __init__
    super().__init__(loop=loop)
  File "/usr/lib/python3.10/asyncio/mixins.py", line 17, in __init__
    raise TypeError(
TypeError: As of 3.10, the *loop* parameter was removed from Lock() since it is no longer necessary

I've looked into websockets/protocol.py:235 and there's already exist some check of python version:

self._drain_lock = asyncio.Lock(
    loop=loop if sys.version_info[:2] < (3, 8) else None
)

But from what I understand, this is not enough to solve this problem.

aaugustin commented 3 years ago

We're going to need something like:

self._drain_lock = asyncio.Lock(
    **({"loop": loop} if sys.version_info[:2] < (3, 8) else {})
)

There's a bunch of other instances of this pattern. I can fix them.

aaugustin commented 3 years ago

Filing #935 for later.

aaugustin commented 3 years ago

So, the big problem is that Python 3.10 deprecates interacting with a non-running event loop, while websockets entire test suite depends on this pattern :-(

malcovitch commented 2 years ago

Hi. Has this been resolved, yet? I'm trying to use websockets with Python 3.10...

aaugustin commented 2 years ago

See 5 lines above ;-)

Nvl977 commented 2 years ago

See 5 lines above ;-)

Hi mate, couldnt seem to find the solution, any chance of a direct link? Fairly new to the WSS side. Is it the thread 'Remove the use of get_event_loop'?

Thanks in advance

aaugustin commented 2 years ago

I was referring to the fact that this issue is fixed and the current version of websockets fully supports Python 3.10.

Patrickchong95823 commented 2 years ago

Hi, I am getting a very similar error when using web sockets to pull Binance API data: "TypeError: As of 3.10, the loop parameter was removed from Queue() since it is no longer necessary". Is there a working solution for the issue? I can't understand the discussion above- from what I gather this is still an ongoing issue?

Patrickchong95823 commented 2 years ago

I was referring to the fact that this issue is fixed and the current version of websockets fully supports Python 3.10.

How do we fix it? I am using Python 3.10 - is there a later version of something I need to install, or ... ?

aaugustin commented 2 years ago

FYI https://websockets.readthedocs.io/en/stable/project/contributing.html#cryptocurrency-users

HectorGitt commented 2 years ago

Hi, I am getting a very similar error when using web sockets to pull Binance API data: "TypeError: As of 3.10, the loop parameter was removed from Queue() since it is no longer necessary". Is there a working solution for the issue? I can't understand the discussion above- from what I gather this is still an ongoing issue?

Upgrade package websockets from 9.1 to latest version

Jessie-1 commented 2 years ago

FYI https://websockets.readthedocs.io/en/stable/project/contributing.html#cryptocurrency-users

bullshit

aaugustin commented 2 years ago

This kind of behavior is exactly why I stopped wasting my time with crypto enthusiasts like you.