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 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(): async def entry():
await asyncio.wait([ await asyncio.gather(
playlistmanage.metadatafetch_loop(), mpv_control.run(),
]) test(),
)
def main(): def main():
asyncio.get_event_loop().run_until_complete( nyasync.run(entry())
entry()
)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -8,6 +8,21 @@ def ify(func):
None, lambda: func(*args, **kwargs)) None, lambda: func(*args, **kwargs))
return asyncified 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): class Queue(asyncio.Queue):
__anext__ = asyncio.Queue.get __anext__ = asyncio.Queue.get
@ -37,12 +52,17 @@ class Condition:
with await self.monitor: with await self.monitor:
self.monitor.notify_all() self.monitor.notify_all()
async def unix_connection(path):
endpoints = await asyncio.open_unix_connection(path)
return UnixConnection(*endpoints)
class UnixConnection: class UnixConnection:
async def __init__(self, path): def __init__(self, reader, writer):
(self.reader, self.writer) = await asyncio.open_unix_connection(path) self.reader = reader
self.writer = writer
def __aiter__(self): def __aiter__(self):
return self.reader.__aiter__() return self.reader.__aiter__()
def write(self, data): def write(self, data):
return self.writer.write(data); self.writer.write(data)