-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsyncer.py
36 lines (26 loc) · 1007 Bytes
/
syncer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import singledispatch, wraps
import asyncio
import inspect
import types
from typing import Any, Callable, Generator
@singledispatch
def sync(co: Any):
raise TypeError('Called with unsupported argument: {}'.format(co))
@sync.register(asyncio.Future)
@sync.register(types.GeneratorType)
def sync_co(co: Generator[Any, None, Any]) -> Any:
if not inspect.isawaitable(co):
raise TypeError('Called with unsupported argument: {}'.format(co))
return asyncio.get_event_loop().run_until_complete(co)
@sync.register(types.FunctionType)
@sync.register(types.MethodType)
def sync_fu(f: Callable[..., Any]) -> Callable[..., Any]:
if not asyncio.iscoroutinefunction(f):
raise TypeError('Called with unsupported argument: {}'.format(f))
@wraps(f)
def run(*args, **kwargs):
return asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))
return run
sync.register(types.CoroutineType)(sync_co)