国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python 使用事件對象asyncio.Event來同步協程的操作

瀏覽:32日期:2022-07-26 13:34:09

事件對象asyncio.Event是基于threading.Event來實現的。

事件可以一個信號觸發多個協程同步工作,

例子如下:

import asyncioimport functools def set_event(event): print(’setting event in callback’) event.set() async def coro1(event): print(’coro1 waiting for event’) await event.wait() print(’coro1 triggered’) async def coro2(event): print(’coro2 waiting for event’) await event.wait() print(’coro2 triggered’) async def main(loop): # Create a shared event event = asyncio.Event() print(’event start state: {}’.format(event.is_set())) loop.call_later( 0.1, functools.partial(set_event, event) ) await asyncio.wait([coro1(event), coro2(event)]) print(’event end state: {}’.format(event.is_set())) event_loop = asyncio.get_event_loop()try: event_loop.run_until_complete(main(event_loop))finally: event_loop.close()

輸出如下:

event start state: Falsecoro2 waiting for eventcoro1 waiting for eventsetting event in callbackcoro2 triggeredcoro1 triggeredevent end state: True

補充知識: python里使用協程來創建echo客戶端

在這個例子里使用asyncio.Protocol來創建一個echo客戶端,先導入庫asyncio和logging。

接著定義發送的消息MESSAGES。

創建連接服務器的地址SERVER_ADDRESS,接著創建EchoClient類,它是繼承asyncio.Protocol。

在這個類的構造函數里,接收兩個參數messages和future,

messages是指定要發送的消息數據,future是用來通知socket接收數據完成或者服務器關閉socket的事件通知,以便事件循環知道這個協程已經完成了,就可以退出整個程序。

connection_made函數是當socket連接到服務器時調用,它就立即發送數據給服務器,數據發送完成之后發送了eof標記。

服務器收到數據和標志都回復客戶端,客戶端data_received函數接收數據,eof_received函數接收結束標記。

connection_lost函數收到服務器斷開連接。

這行代碼:

client_completed = asyncio.Future()

創建一個協程完成的觸發事件。

由于event_loop.create_connection函數只能接收一個參數,需要使用functools.partial來進行多個參數包裝成一個參數。

后面通過事件循環來運行協程。

import asyncioimport functoolsimport loggingimport sys MESSAGES = [ b’This is the message. ’, b’It will be sent ’, b’in parts.’,]SERVER_ADDRESS = (’localhost’, 10000) class EchoClient(asyncio.Protocol): def __init__(self, messages, future): super().__init__() self.messages = messages self.log = logging.getLogger(’EchoClient’) self.f = future def connection_made(self, transport): self.transport = transport self.address = transport.get_extra_info(’peername’) self.log.debug( ’connecting to {} port {}’.format(*self.address) ) # This could be transport.writelines() except that # would make it harder to show each part of the message # being sent. for msg in self.messages: transport.write(msg) self.log.debug(’sending {!r}’.format(msg)) if transport.can_write_eof(): transport.write_eof() def data_received(self, data): self.log.debug(’received {!r}’.format(data)) def eof_received(self): self.log.debug(’received EOF’) self.transport.close() if not self.f.done(): self.f.set_result(True) def connection_lost(self, exc): self.log.debug(’server closed connection’) self.transport.close() if not self.f.done(): self.f.set_result(True) super().connection_lost(exc) logging.basicConfig( level=logging.DEBUG, format=’%(name)s: %(message)s’, stream=sys.stderr,)log = logging.getLogger(’main’) event_loop = asyncio.get_event_loop() client_completed = asyncio.Future() client_factory = functools.partial( EchoClient, messages=MESSAGES, future=client_completed,)factory_coroutine = event_loop.create_connection( client_factory, *SERVER_ADDRESS,) log.debug(’waiting for client to complete’)try: event_loop.run_until_complete(factory_coroutine) event_loop.run_until_complete(client_completed)finally: log.debug(’closing event loop’) event_loop.close()

以上這篇python 使用事件對象asyncio.Event來同步協程的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 韩国免费特一级毛片 | 亚洲精品免费视频 | 亚洲成人福利在线 | 欧美成在线 | 波多野结衣一区二区 三区 波多野结衣一区二区三区88 | 中文字幕一区二区三区免费视频 | 99精品福利视频在线一区 | 久久亚洲国产成人亚 | 国外精品视频在线观看免费 | 久久久久久久久久久久久久久久久久 | 国产精品欧美韩国日本久久 | 亚洲最大黄网 | 最新国产美女肝交视频播放 | 国产美女高清一级a毛片 | 国产一级二级三级视频 | 久久影院视频 | 欧美 自拍| 国产理论在线观看 | 有码日韩 | 免费国产午夜高清在线视频 | 亚洲国产成人精彩精品 | 国产成人精品一区二区不卡 | 国产成人精品曰本亚洲78 | 黑人特黄aa毛片 | 手机在线成人精品视频网 | 欧美日韩无 | 青青青免费手机版视频在线观看 | 拍真实国产伦偷精品 | 香蕉97碰碰视频免费 | 九草视频在线观看 | 亚洲美女视频网址 | 美国一级免费毛片 | 久久欧美成人精品丝袜 | 欧洲美女与男人做爰 | 毛片免费观看日本中文 | 国产日本韩国不卡在线视频 | 久久亚洲不卡一区二区 | a亚洲天堂 | 波多野一区二区三区在线 | 日本二区免费一片黄2019 | 亚洲欧美日韩精品久久久 |