如何编写可选择作为常规函数的asyncio协同程序?

时间:2015-05-10 18:31:03

标签: python python-3.x python-asyncio

我正在编写一个我希望最终用户可以选择使用的库,就像它的方法和函数不是协同程序一样。

例如,给定此功能:

@asyncio.coroutine
def blah_getter():
    return (yield from http_client.get('http://blahblahblah'))

不关心在自己的代码中使用任何异步功能的最终用户仍然需要导入asyncio并运行它:

>>> response = asyncio.get_event_loop().run_until_complete(blah_getter())

如果可以的话,我会在blah_getter内确定我是否被称为协程,并做出相应的反应。

类似于:

@asyncio.coroutine
def blah_getter():
    if magically_determine_if_being_yielded_from():
        return (yield from http_client.get('http://blahblahblah'))
    else:
        el = asyncio.get_event_loop()
        return el.run_until_complete(http_client.get('http://blahblahblah'))

3 个答案:

答案 0 :(得分:24)

您需要两个函数 - 异步协程和同步常规函数:

@asyncio.coroutine
def async_gettter():
    return (yield from http_client.get('http://example.com'))

def sync_getter()
    return asyncio.get_event_loop().run_until_complete(async_getter())

magically_determine_if_being_yielded_from()实际上是event_loop.is_running()但我强烈建议不要在同一个函数中混用同步和异步代码。

答案 1 :(得分:15)

我同意Andrew的回答,我只想补充一点,如果您处理的是对象而不是顶层函数,则可以使用元类自动添加异步方法的同步版本。见这个例子:

import asyncio
import aiohttp

class SyncAdder(type):
    """ A metaclass which adds synchronous version of coroutines.

    This metaclass finds all coroutine functions defined on a class
    and adds a synchronous version with a '_s' suffix appended to the
    original function name.

    """
    def __new__(cls, clsname, bases, dct, **kwargs):
        new_dct = {}
        for name,val in dct.items():
            # Make a sync version of all coroutine functions
            if asyncio.iscoroutinefunction(val):
                meth = cls.sync_maker(name)
                syncname = '{}_s'.format(name)
                meth.__name__ = syncname
                meth.__qualname__ = '{}.{}'.format(clsname, syncname)
                new_dct[syncname] = meth
        dct.update(new_dct)
        return super().__new__(cls, clsname, bases, dct)

    @staticmethod
    def sync_maker(func):
        def sync_func(self, *args, **kwargs):
            meth = getattr(self, func)
            return asyncio.get_event_loop().run_until_complete(meth(*args, **kwargs))
        return sync_func

class Stuff(metaclass=SyncAdder):
    @asyncio.coroutine
    def getter(self, url):
        return (yield from aiohttp.request('GET', url))

用法:

>>> import aio, asyncio
>>> aio.Stuff.getter_s
<function Stuff.getter_s at 0x7f90459c2bf8>
>>> aio.Stuff.getter
<function Stuff.getter at 0x7f90459c2b70>
>>> s = aio.Stuff()
>>> s.getter_s('http://example.com')
<ClientResponse(http://example.com) [200 OK]>
<CIMultiDictProxy {'ACCEPT-RANGES': 'bytes', 'CACHE-CONTROL': 'max-age=604800', 'DATE': 'Mon, 11 May 2015 15:13:21 GMT', 'ETAG': '"359670651"', 'EXPIRES': 'Mon, 18 May 2015 15:13:21 GMT', 'SERVER': 'ECS (ewr/15BD)', 'X-CACHE': 'HIT', 'X-EC-CUSTOM-ERROR': '1', 'CONTENT-LENGTH': '1270', 'CONTENT-TYPE': 'text/html', 'LAST-MODIFIED': 'Fri, 09 Aug 2013 23:54:35 GMT', 'VIA': '1.1 xyz.com:80', 'CONNECTION': 'keep-alive'}>
>>> asyncio.get_event_loop().run_until_complete(s.getter('http://example.com'))
<ClientResponse(http://example.com) [200 OK]>
<CIMultiDictProxy {'ACCEPT-RANGES': 'bytes', 'CACHE-CONTROL': 'max-age=604800', 'DATE': 'Mon, 11 May 2015 15:25:09 GMT', 'ETAG': '"359670651"', 'EXPIRES': 'Mon, 18 May 2015 15:25:09 GMT', 'SERVER': 'ECS (ewr/15BD)', 'X-CACHE': 'HIT', 'X-EC-CUSTOM-ERROR': '1', 'CONTENT-LENGTH': '1270', 'CONTENT-TYPE': 'text/html', 'LAST-MODIFIED': 'Fri, 09 Aug 2013 23:54:35 GMT', 'VIA': '1.1 xys.com:80', 'CONNECTION': 'keep-alive'}>

答案 2 :(得分:0)

您还可以创建一个简单的装饰器,使您的功能同步。这种方法可以应用于全局函数和方法。

一个例子。

# the decorator
def sync(f):
    ASYNC_KEY = 'async'

    def f_in(*args, **kwargs):
        if ASYNC_KEY in kwargs:
            async = kwargs.get(ASYNC_KEY)
            del kwargs[ASYNC_KEY]
        else:
            async = True

        if async:
            return f(*args, **kwargs)           
        else:
            return asyncio.get_event_loop().run_until_complete(f())

    return f_in

# below: the usage    
@sync
async def test():
    print('In sleep...')
    await asyncio.sleep(1)
    print('After sleep')    


# below: or asyncio.get_event_loop().create_task(test())
asyncio.get_event_loop().run_until_complete(test()) 
# and here is your syncronious version
test(async=False)

此外:创建特殊的包装类可能有意义,不要将async传递给每个方法调用。示例如下。

class SyncCallerWrapper(object):
    def __init__(self, obj, is_async=True):
        self._obj = obj 
        self._is_async = is_async


    def __getattr__(self, name):
        def sync_wrapper(obj_attr):
            def f(*args, **kwargs):
                return asyncio.get_event_loop().run_until_complete(obj_attr(*args, **kwargs))

            return f

        obj_attr = getattr(self._obj, name)

        if not self._is_async and asyncio.iscoroutinefunction(obj_attr):
            return sync_wrapper(obj_attr)           

        return obj_attr


class C(object):
    async def sleep1(self):
        print('In sleep1...')
        await asyncio.sleep(1)
        print('After sleep1')


    async def sleep2(self):
        print('In sleep2...')
        await asyncio.sleep(1)
        print('After sleep2')       


# you don't want any concurrency in your code
c_sync = SyncCallerWrapper(C(), is_async=False)
c_sync.sleep1()
c_sync.sleep2()

# here you want concurrency: class methods are coroutines
c_async = SyncCallerWrapper(C(), is_async=True)
asyncio.get_event_loop().run_until_complete(c_async.sleep1())
asyncio.get_event_loop().run_until_complete(c_async.sleep2())

为了更优雅,您可以使用函数(全局构造函数)替换您的类。然后,用户可以创建传递C参数的类is_async并具有所需的行为:方法将充当常规(is_async=False)或async函数(is_async=True

def C(*args, **kwargs):
    KEY_ISASYNC = 'is_async'
    if KEY_ISASYNC in kwargs:
        is_async = kwargs.get(KEY_ISASYNC)
        del kwargs[KEY_ISASYNC]
    else:
        is_async = False
    return SyncCallerWrapper(_C(*args, **kwargs), is_async=is_async)

# you don't want any concurrency in your code
c_sync = C(is_async=False)
c_sync.sleep1()
c_sync.sleep2()

# here you want concurrency: class methods are coroutines
c_async = C(is_async=True)
asyncio.get_event_loop().run_until_complete(c_async.sleep1())
asyncio.get_event_loop().run_until_complete(c_async.sleep2())