Asyncio.Semaphore RuntimeError: Die Aufgabe hat Future an eine andere Schleife angehängtPython

Python-Programme
Anonymous
 Asyncio.Semaphore RuntimeError: Die Aufgabe hat Future an eine andere Schleife angehängt

Post by Anonymous »

Wenn ich diesen Code in Python 3.7 ausführe:

Code: Select all

import asyncio

sem = asyncio.Semaphore(2)

async def work():
async with sem:
print('working')
await asyncio.sleep(1)

async def main():
await asyncio.gather(work(), work(), work())

asyncio.run(main())
Es schlägt mit RuntimeError fehl:

Code: Select all

$ python3 demo.py
working
working
Traceback (most recent call last):
File "demo.py", line 13, in 
asyncio.run(main())
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "demo.py", line 11, in main
await asyncio.gather(work(), work(), work())
File "demo.py", line 6, in work
async with sem:
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/locks.py", line 92, in __aenter__
await self.acquire()
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/locks.py", line 474, in acquire
await fut
RuntimeError: Task  got Future  attached to a different loop

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post