Git Product home page Git Product logo

Comments (11)

asvetlov avatar asvetlov commented on August 25, 2024 2

janus 0.6.2 released with the fix

from janus.

simonw avatar simonw commented on August 25, 2024 1

Could this be a bug in Python 3.10?

from janus.

simonw avatar simonw commented on August 25, 2024 1

That addressed the bug!

diff --git a/janus/__init__.py b/janus/__init__.py
index 789f12f..a2375b6 100644
--- a/janus/__init__.py
+++ b/janus/__init__.py
@@ -36,6 +36,8 @@ class Queue(Generic[T]):
         self._all_tasks_done = threading.Condition(self._sync_mutex)
 
         self._async_mutex = asyncio.Lock()
+        # Workaround for issue #358:
+        getattr(self._async_mutex, '_get_loop', lambda: None)()
         self._async_not_empty = asyncio.Condition(self._async_mutex)
         self._async_not_full = asyncio.Condition(self._async_mutex)
         self._finished = asyncio.Event()
(janus) janus % pytest -x      
============================================================= test session starts ==============================================================
platform darwin -- Python 3.10.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/simon/Dropbox/Development/janus
plugins: cov-2.12.1, asyncio-0.15.1
collected 72 items                                                                                                                             

tests/test_async.py ...............................                                                                                      [ 43%]
tests/test_mixed.py ..................                                                                                                   [ 68%]
tests/test_sync.py .......................                                                                                               [100%]

============================================================== 72 passed in 5.34s ==============================================================

from janus.

simonw avatar simonw commented on August 25, 2024

The code involved here is the Queue constructor:

janus/janus/__init__.py

Lines 24 to 42 in d7970f8

class Queue(Generic[T]):
def __init__(self, maxsize: int = 0) -> None:
self._loop = current_loop()
self._maxsize = maxsize
self._init(maxsize)
self._unfinished_tasks = 0
self._sync_mutex = threading.Lock()
self._sync_not_empty = threading.Condition(self._sync_mutex)
self._sync_not_full = threading.Condition(self._sync_mutex)
self._all_tasks_done = threading.Condition(self._sync_mutex)
self._async_mutex = asyncio.Lock()
self._async_not_empty = asyncio.Condition(self._async_mutex)
self._async_not_full = asyncio.Condition(self._async_mutex)
self._finished = asyncio.Event()
self._finished.set()

from janus.

simonw avatar simonw commented on August 25, 2024

In particular it seems to be these two lines:

janus/janus/__init__.py

Lines 38 to 39 in d7970f8

self._async_mutex = asyncio.Lock()
self._async_not_empty = asyncio.Condition(self._async_mutex)

I don't understand how those are any different from doing this: asyncio.Condition(asyncio.Lock()) - which works fine when I try it in the Python 3.10 interpreter:

~ % /Users/simon/.pyenv/versions/3.10.0/bin/python
Python 3.10.0 (default, Oct  7 2021, 13:45:58) [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> asyncio.Condition(asyncio.Lock())
<asyncio.locks.Condition object at 0x10ace2110 [unlocked]>

from janus.

simonw avatar simonw commented on August 25, 2024

Aha! But that simple asyncio.Condition(asyncio.Lock()) line DOES fail if it runs inside an event loop:

>>> async def run():
...     print(asyncio.Condition(asyncio.Lock()))
... 
>>> asyncio.run(run())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/simon/.pyenv/versions/3.10.0/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/simon/.pyenv/versions/3.10.0/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete
    return future.result()
  File "<stdin>", line 2, in run
  File "/Users/simon/.pyenv/versions/3.10.0/lib/python3.10/asyncio/locks.py", line 234, in __init__
    raise ValueError("loop argument must agree with lock")
ValueError: loop argument must agree with lock

from janus.

simonw avatar simonw commented on August 25, 2024

I filed a Python 3.10 issue about this here: https://bugs.python.org/issue45416

from janus.

ambv avatar ambv commented on August 25, 2024

I commented on BPO-45416 with a workaround that janus can employ.

from janus.

simonw avatar simonw commented on August 25, 2024

Here's the first of @ambv's suggested workaround (there's a longer but less hacky one in the comment too):

>>> l = asyncio.Lock()
>>> getattr(l, '_get_loop', lambda: None)()

I'm going to try that against Janus now.

from janus.

achimnol avatar achimnol commented on August 25, 2024

I have sent a PR to CPython: python/cpython#28850.

from janus.

StanleyAbotsikuma avatar StanleyAbotsikuma commented on August 25, 2024

i have same error with django channels

from janus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.