Use asyncio.gather instead of asyncio.wait

asyncio.gather immediatly propagates exceptions, which is nice for
debugging, and what we wanted in the first place.
This commit is contained in:
Aleksander Wasaznik 2017-02-06 20:22:01 +01:00
parent 2f130874f7
commit 2b33f51053
2 changed files with 36 additions and 10 deletions

View File

@ -1,15 +1,21 @@
import asyncio
from . import playlistmanage
from . import mpv
from . import nyasync
mpv_control = mpv.MPVControl()
async def test():
await mpv_control.send_request({"command":["loadfile",'grzegorz/res/logo.jpg']})
async def entry():
await asyncio.wait([
playlistmanage.metadatafetch_loop(),
])
await asyncio.gather(
mpv_control.run(),
test(),
)
def main():
asyncio.get_event_loop().run_until_complete(
entry()
)
nyasync.run(entry())
if __name__ == '__main__':
main()

View File

@ -8,6 +8,21 @@ def ify(func):
None, lambda: func(*args, **kwargs))
return asyncified
def safely(func, *args, **kwargs):
asyncloop = asyncio.get_event_loop()
asyncloop.call_soon_threadsafe(lambda: func(*args, **kwargs))
def callback(coro, callback=None):
asyncloop = asyncio.get_event_loop()
future = asyncio.run_cooroutine_threadsafe(coro, asyncloop)
if callback:
future.add_done_callback(callback)
return future
def run(*coros):
asyncloop = asyncio.get_event_loop()
return asyncloop.run_until_complete(asyncio.gather(*coros))
class Queue(asyncio.Queue):
__anext__ = asyncio.Queue.get
@ -37,12 +52,17 @@ class Condition:
with await self.monitor:
self.monitor.notify_all()
async def unix_connection(path):
endpoints = await asyncio.open_unix_connection(path)
return UnixConnection(*endpoints)
class UnixConnection:
async def __init__(self, path):
(self.reader, self.writer) = await asyncio.open_unix_connection(path)
def __init__(self, reader, writer):
self.reader = reader
self.writer = writer
def __aiter__(self):
return self.reader.__aiter__()
def write(self, data):
return self.writer.write(data);
self.writer.write(data)