python 使用事件對(duì)象asyncio.Event來同步協(xié)程的操作
事件對(duì)象asyncio.Event是基于threading.Event來實(shí)現(xiàn)的。
事件可以一個(gè)信號(hào)觸發(fā)多個(gè)協(xié)程同步工作,
例子如下:
import asyncio import 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: False coro2 waiting for event coro1 waiting for event setting event in callback coro2 triggered coro1 triggered event end state: True
補(bǔ)充知識(shí): python里使用協(xié)程來創(chuàng)建echo客戶端
在這個(gè)例子里使用asyncio.Protocol來創(chuàng)建一個(gè)echo客戶端,先導(dǎo)入庫(kù)asyncio和logging。
接著定義發(fā)送的消息MESSAGES。
創(chuàng)建連接服務(wù)器的地址SERVER_ADDRESS,接著創(chuàng)建EchoClient類,它是繼承asyncio.Protocol。
在這個(gè)類的構(gòu)造函數(shù)里,接收兩個(gè)參數(shù)messages和future,
messages是指定要發(fā)送的消息數(shù)據(jù),future是用來通知socket接收數(shù)據(jù)完成或者服務(wù)器關(guān)閉socket的事件通知,以便事件循環(huán)知道這個(gè)協(xié)程已經(jīng)完成了,就可以退出整個(gè)程序。
connection_made函數(shù)是當(dāng)socket連接到服務(wù)器時(shí)調(diào)用,它就立即發(fā)送數(shù)據(jù)給服務(wù)器,數(shù)據(jù)發(fā)送完成之后發(fā)送了eof標(biāo)記。
服務(wù)器收到數(shù)據(jù)和標(biāo)志都回復(fù)客戶端,客戶端data_received函數(shù)接收數(shù)據(jù),eof_received函數(shù)接收結(jié)束標(biāo)記。
connection_lost函數(shù)收到服務(wù)器斷開連接。
這行代碼:
client_completed = asyncio.Future()
創(chuàng)建一個(gè)協(xié)程完成的觸發(fā)事件。
由于event_loop.create_connection函數(shù)只能接收一個(gè)參數(shù),需要使用functools.partial來進(jìn)行多個(gè)參數(shù)包裝成一個(gè)參數(shù)。
后面通過事件循環(huán)來運(yùn)行協(xié)程。
import asyncio import functools import logging import 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 使用事件對(duì)象asyncio.Event來同步協(xié)程的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中PyQt5可視化界面通過拖拽來上傳文件的實(shí)現(xiàn)
本文主要介紹了Python中PyQt5可視化界面通過拖拽來上傳文件的實(shí)現(xiàn),通過構(gòu)建一個(gè)可接受拖拽的區(qū)域,并重寫相關(guān)事件處理函數(shù),可以方便地實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12Python實(shí)現(xiàn)圖片批量加入水印代碼實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)圖片批量加入水印代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11使用python-magic和wxPython實(shí)現(xiàn)識(shí)別文檔類型
這篇文章主要介紹了如何使用python-magic模塊和wxPython庫(kù)創(chuàng)建一個(gè)簡(jiǎn)單的文件列表應(yīng)用程序,該應(yīng)用程序可以顯示所選文件夾中文件的類型,需要的可以參考下2023-08-08Python進(jìn)度條神器tqdm使用實(shí)例詳解
Python進(jìn)度條神器tqdm是一個(gè)快速、可擴(kuò)展的進(jìn)度條工具,可以輕松地為Python腳本添加進(jìn)度條。它可以在循環(huán)中自動(dòng)計(jì)算進(jìn)度,并在終端中顯示進(jìn)度條,讓用戶了解程序的運(yùn)行情況。tqdm還支持多線程和多進(jìn)程,并且可以自定義進(jìn)度條的樣式和顯示方式。2023-06-06python高并發(fā)異步服務(wù)器核心庫(kù)forkcore使用方法
這篇文章主要介紹了python高并發(fā)異步服務(wù)器核心庫(kù)forkcore的使用方法,大家參考使用吧2013-11-11python實(shí)現(xiàn)輸入數(shù)字的連續(xù)加減方法
今天小編就為大家分享一篇python實(shí)現(xiàn)輸入數(shù)字的連續(xù)加減方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)批量圖片格式轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06