Event loop¶
ãœãŒã¹ã³ãŒã: Lib/asyncio/profile.py ãš Lib/asyncio/pstats.py
ãŸããã
ã€ãã³ãã«ãŒãã¯å šãŠã® asyncio ã¢ããªã±ãŒã·ã§ã³ã®äžæ žããªãååšã§ããã€ãã³ãã«ãŒãã¯éåæã¿ã¹ã¯ãã³ãŒã«ããã¯ãå®è¡ãããããã¯ãŒã¯ I/O ãåŠçãããµãããã»ã¹ãå®è¡ããŸãã
ã¢ããªã±ãŒã·ã§ã³éçºè
ã¯éåžž asyncio.run() ã®ãããªé«æ°Žæºã® ayncio 颿°ã ããå©çšããã«ãŒããªããžã§ã¯ããåç
§ããããã«ãŒããªããžã§ã¯ãã®ã¡ãœãããåŒã³åºãããããããšã¯ã»ãšãã©ãããŸããããã®ç¯ã¯ãã€ãã³ãã«ãŒãã®æ¯ãèãã«å¯ŸããŠçްãã調æŽãå¿
èŠãªãäœæ°Žæºã®ã³ãŒããã©ã€ãã©ãªããã¬ãŒã ã¯ãŒã¯ã®éçºè
åãã§ãã
ã€ãã³ãã«ãŒãã®ååŸ
以äžã®äœæ°Žæºé¢æ°ã¯ã€ãã³ãã«ãŒãã®ååŸãèšå®ãçæããããã«äœ¿ããŸã:
- asyncio.get_running_loop()¶
çŸåšã® OS ã¹ã¬ããã§å®è¡äžã®ã€ãã³ãã«ãŒããååŸããŸãã
Raise a
RuntimeErrorif there is no running event loop.This function can only be called from a coroutine or a callback.
Added in version 3.7.
- asyncio.get_event_loop()¶
çŸåšã®ã€ãã³ãã«ãŒããååŸããŸãã
When called from a coroutine or a callback (e.g. scheduled with call_soon or similar API), this function will always return the running event loop.
If there is no running event loop set, the function will return the result of the
get_event_loop_policy().get_event_loop()call.ãã®é¢æ°ã®æ¯ãèã㯠(ç¹ã«ã€ãã³ãã«ãŒãããªã·ãŒãã«ã¹ã¿ãã€ãºããå Žå) è€éãªãããã³ã«ãŒãã³ãã³ãŒã«ããã¯ã§ã¯
get_event_loop()ãããget_running_loop()ã䜿ãã»ãã奜ãŸãããšèããããŸããAs noted above, consider using the higher-level
asyncio.run()function, instead of using these lower level functions to manually create and close an event loop.ããŒãžã§ã³ 3.14 ã§å€æŽ: Raises a
RuntimeErrorif there is no current event loop.泚é
The
asynciopolicy system is deprecated and will be removed in Python 3.16; from there on, this function will return the current running event loop if present else it will return the loop set byset_event_loop().
- asyncio.set_event_loop(loop)¶
loop ã OS ã¹ã¬ããã®çŸåšã®ã€ãã³ãã«ãŒãã«èšå®ããŸãã
- asyncio.new_event_loop()¶
æ°ããã€ãã³ãã«ãŒããªããžã§ã¯ããçæããŠè¿ããŸãã
get_event_loop(), set_event_loop(), ããã³ new_event_loop() 颿°ã®æ¯ãèãã¯ã ã«ã¹ã¿ã ã€ãã³ãã«ãŒãããªã·ãŒãèšå®ãã ããšã«ãã倿Žããããšãã§ããŸãã
å 容
ãã®ããŒãžã¯ä»¥äžã®ç¯ããæ§æãããŸã:
ã€ãã³ãã«ãŒãã®ã¡ãœãã ç¯ã¯ãã€ãã³ãã«ãŒã API ã®ãªãã¡ã¬ã³ã¹ã§ãã
ã³ãŒã«ããã¯ãã³ãã« ç¯ã¯
loop.call_soon()ãloop.call_later()ãªã©ã®ã¹ã±ãžã¥ãŒãªã³ã°ã¡ãœãããè¿ãHandleãTimerHandleã€ã³ã¹ã¿ã³ã¹ã«ã€ããŠè§£èª¬ããŠããŸãããµãŒããŒãªããžã§ã¯ã ç¯ã¯
loop.create_server()ã®ãããªã¡ãœãããè¿ãåã«ã€ããŠè§£èª¬ããŠããŸããã€ãã³ãã«ãŒãã®å®è£ ç¯ã¯
SelectorEventLoopãšProactorEventLoopã®2ã€ã®ã¯ã©ã¹ã«ã€ããŠè§£èª¬ããŠããŸãã䜿çšäŸ ç¯ã§ã¯ã€ãã³ãã«ãŒã API ã®å ·äœçãªäœ¿ãæ¹ã玹ä»ããŠããŸãã
Event loop methods¶
ã€ãã³ãã«ãŒãã¯ä»¥äžã® äœæ°Žæºãª API ãæã£ãŠããŸã:
ã«ãŒãã®éå§ãšåæ¢Â¶
- loop.run_until_complete(future)¶
ãã¥ãŒãã£ãŒ (
Futureã€ã³ã¹ã¿ã³ã¹) ãå®äºãããŸã§å®è¡ããŸããåŒæ°ã ã³ã«ãŒãã³ãªããžã§ã¯ã ã®å Žåãæé»ã®ãã¡ã«
asyncio.TaskãšããŠå®è¡ãããããã«ã¹ã±ãžã¥ãŒã«ãããŸããFuture ã®çµæãè¿ãããäŸå€ãéåºããŸãã
- loop.run_forever()¶
stop()ãåŒã³åºããããŸã§ã€ãã³ãã«ãŒããå®è¡ããŸããrun_forever()ã¡ãœãããåŒã°ããããåã«stop()ã¡ãœãããåŒã°ããå Žåãã€ãã³ãã«ãŒãã¯ã¿ã€ã ã¢ãŠãããŒãã«ããŠäžåºŠã ã I/O ã»ã¬ã¯ã¿ã®åãåããåŠçãè¡ãã I/O ã€ãã³ãã«å¯ŸããŠã¹ã±ãžã¥ãŒã«ãããå šãŠã®ã³ãŒã«ãã㯠(ããã³æ¢ã«ã¹ã±ãžã¥ãŒã«æžã¿ã®ã³ãŒã«ããã¯) ãå®è¡ããã®ã¡ãçµäºããŸããrun_forever()ã¡ãœãããå®è¡äžã«stop()ã¡ãœãããåŒã³åºãããå Žåãã€ãã³ãã«ãŒãã¯çŸåšåŠçãããŠãããã¹ãŠã®ã³ãŒã«ããã¯ãå®è¡ããŠããçµäºããŸãã ãã®å Žåãã³ãŒã«ããã¯ã«ããæ°ãã«ã¹ã±ãžã¥ãŒã«ãããã³ãŒã«ããã¯ã¯å®è¡ãããªãããšã«æ³šæããŠãã ãã; ãããæ°ãã«ã¹ã±ãžã¥ãŒã«ãããã³ãŒã«ããã¯ã¯ã次ã«run_forever()ãŸãã¯run_until_complete()ãåŒã³åºããããšãã«å®è¡ãããŸãã
- loop.stop()¶
ã€ãã³ãã«ãŒãã忢ããŸãã
- loop.is_running()¶
ã€ãã³ãã«ãŒããçŸåšå®è¡äžã®å Žå
Trueãè¿ããŸãã
- loop.is_closed()¶
ã€ãã³ãã«ãŒããéããããŠããå Žå
Trueãè¿ããŸãã
- loop.close()¶
ã€ãã³ãã«ãŒããã¯ããŒãºããŸãã
ãã®é¢æ°ãåŒã³åºãããæç¹ã§ãã€ãã³ãã«ãŒããå®è¡äžã§ãã£ãŠã¯ãããŸãããä¿çäžã®ã³ãŒã«ããã¯ã¯ãã¹ãŠç Žæ£ãããŸãã
ãã®ã¡ãœããã¯å šãŠã®ãã¥ãŒãã¯ãªã¢ãããšã°ãŒãã¥ãŒã¿ãŒãå®è¡å®äºããã®ãåŸ ããã«ã·ã£ããããŠã³ããŸãã
ãã®ã¡ãœããã¯ã¹ãç (äœåå®è¡ããŠãçµæã¯åã) ã§ããåãæ¶ããŸãããã€ãã³ãã«ãŒããã¯ããŒãºãããåŸãä»ã®ãããªãã¡ãœãããåŒã³åºãã¹ãã§ã¯ãããŸããã
- async loop.shutdown_asyncgens()¶
çŸåšãªãŒãã³ã«ãªã£ãŠãããã¹ãŠã® asynchronous generator (éåæãžã§ãã¬ãŒã¿) ãªããžã§ã¯ããã¹ã±ãžã¥ãŒã«ãã
aclose()ã¡ãœãããåŒã³åºãããšã§ããããã¯ããŒãºããŸãã ãã®ã¡ãœããã®åŒã³åºãåŸã«æ°ããéåæãžã§ãã¬ãŒã¿ãã€ãã¬ãŒãããããšãã€ãã³ãã«ãŒãã¯èŠåãçºããŸãããã®ã¡ãœããã¯ã¹ã±ãžã¥ãŒã«ããããã¹ãŠã®éåæãžã§ãã¬ãŒã¿ã®çµäºåŠçã確å®ã«è¡ãããã«äœ¿çšãã¹ãã§ããasyncio.run()ã䜿ã£ãå Žåã¯ãã®é¢æ°ãåŒã³åºãå¿ èŠã¯ãããŸããã以äžã¯ããã°ã©ã äŸã§ã:
try: loop.run_forever() finally: loop.run_until_complete(loop.shutdown_asyncgens()) loop.close()
Added in version 3.6.
- async loop.shutdown_default_executor(timeout=None)¶
Schedule the closure of the default executor and wait for it to join all of the threads in the
ThreadPoolExecutor. Once this method has been called, using the default executor withloop.run_in_executor()will raise aRuntimeError.The timeout parameter specifies the amount of time (in
floatseconds) the executor will be given to finish joining. With the default,None, the executor is allowed an unlimited amount of time.If the timeout is reached, a
RuntimeWarningis emitted and the default executor is terminated without waiting for its threads to finish joining.泚é
Do not call this method when using
asyncio.run(), as the latter handles default executor shutdown automatically.Added in version 3.9.
ããŒãžã§ã³ 3.12 ã§å€æŽ: timeout åŒæ°ã远å ãããŸããã
ã³ãŒã«ããã¯ã®ã¹ã±ãžã¥ãŒãªã³ã°Â¶
- loop.call_soon(callback, *args, context=None)¶
ã€ãã³ãã«ãŒãã®æ¬¡ã®ã€ãã¬ãŒã·ã§ã³ã§ callback ã«æå®ããã³ãŒã«ãã㯠(callback) ã args åŒæ°ã§åŒã³åºãããã«ã¹ã±ãžã¥ãŒã«ããŸãã
asyncio.Handleã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãããã®ã€ã³ã¹ã¿ã³ã¹ã䜿ã£ãŠã¹ã±ãžã¥ãŒã«ããã³ãŒã«ããã¯ããã£ã³ã»ã«ããããšãã§ããŸããã³ãŒã«ããã¯ã¯ç»é²ãããé ã«åŒã³åºãããŸããåã³ãŒã«ããã¯ã¯å³å¯ã«1åã ãåŒã³åºãããŸãã
The optional keyword-only context argument specifies a custom
contextvars.Contextfor the callback to run in. Callbacks use the current context when no context is provided.Unlike
call_soon_threadsafe(), this method is not thread-safe.
- loop.call_soon_threadsafe(callback, *args, context=None)¶
A thread-safe variant of
call_soon(). When scheduling callbacks from another thread, this function must be used, sincecall_soon()is not thread-safe.This function is safe to be called from a reentrant context or signal handler, however, it is not safe or fruitful to use the returned handle in such contexts.
ãã§ã«ã¯ããŒãºãããã€ãã³ãã«ãŒãã«å¯ŸããŠãã®ã¡ãœãããåŒã³åºãããå Žå
RuntimeErroräŸå€ãéåºããŸããããã¯ã¡ã€ã³ã¢ããªã±ãŒã·ã§ã³ãçµäºããŠããã«ããããããã»ã«ã³ããªã¹ã¬ããã§ã¡ãœãããåŒã³åºããããšãã£ãå Žåã«èµ·ããããŸãããã®ããã¥ã¡ã³ã㮠䞊è¡åŠçãšãã«ãã¹ã¬ããåŠç ç¯ãåç §ããŠãã ããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ããŒã¯ãŒãåŒæ° context ã远å ãããŸããã詳现㯠PEP 567 ãåç §ããŠãã ããã
泚é
ã»ãšãã©ã® asyncio ã¢ãžã¥ãŒã«ã®ã¹ã±ãžã¥ãŒãªã³ã°é¢æ°ã¯ãããŒã¯ãŒãåŒæ°ãã³ãŒã«ããã¯ã«æž¡ãããšãèš±ããŠããŸãããããŒã¯ãŒãåŒæ°ãæž¡ãããã«ã¯ functools.partial() ã䜿ã£ãŠãã ãã:
# will schedule "print("Hello", flush=True)"
loop.call_soon(
functools.partial(print, "Hello", flush=True))
asyncio 㯠partial ãªããžã§ã¯ãã®ãããã°ã¡ãã»ãŒãžããšã©ãŒã¡ãã»ãŒãžãããããå¯èŠåããããšãã§ãããããéåžžã¯ã©ã ãåŒããã partial ãªããžã§ã¯ããäœ¿ãæ¹ã䟿å©ã§ãã
é å»¶ã³ãŒã«ããã¯ã®ã¹ã±ãžã¥ãŒãªã³ã°Â¶
ã€ãã³ãã«ãŒãã¯ãã³ãŒã«ããã¯é¢æ°ãæªæ¥ã®ããæç¹ã§åŒã³åºãããããã«ã¹ã±ãžã¥ãŒã«ããä»çµã¿ãæäŸããŸããã€ãã³ãã«ãŒãã¯æå»ãæ»ããªãåèª¿ãªæèš (monotonic clock) ã䜿ã£ãŠæå»ã远跡ããŸãã
- loop.call_later(delay, callback, *args, context=None)¶
delay ç§çµéåŸã«ã³ãŒã«ããã¯é¢æ° callback ãåŒã³åºãããã«ã¹ã±ãžã¥ãŒã«ããŸãã delay ã«ã¯æŽæ°ãŸãã¯æµ®åå°æ°ç¹æ°ãæå®ããŸãã
asyncio.TimerHandleã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãããã®ã€ã³ã¹ã¿ã³ã¹ã䜿ã£ãŠã¹ã±ãžã¥ãŒã«ããã³ãŒã«ããã¯ããã£ã³ã»ã«ããããšãã§ããŸããcallback ã¯å³å¯ã«äžåºŠã ãåŒã³åºãããŸãã2ã€ã®ã³ãŒã«ããã¯ãå®å šã«åãæéã«ã¹ã±ãžã¥ãŒã«ãããå ŽåãåŒã³åºãã®é åºã¯æªå®çŸ©ã§ãã
The optional positional args will be passed to the callback when it is called. Use
functools.partial()to pass keyword arguments to callback.ãªãã·ã§ã³ã®ããŒã¯ãŒãåŒæ° context ã䜿ã£ãŠãã³ãŒã«ããã¯*callback* ãå®è¡ããéã®ã³ã³ããã¹ã
contextvars.Contextãèšå®ããããšãã§ããŸããã³ã³ããã¹ã context ãæå®ãããªãå Žåã¯çŸåšã®ã³ã³ããã¹ãã䜿ãããŸããæ³šé
For performance, callbacks scheduled with
loop.call_later()may run up to one clock-resolution early (seetime.get_clock_info('monotonic').resolution).ããŒãžã§ã³ 3.7 ã§å€æŽ: ããŒã¯ãŒãåŒæ° context ã远å ãããŸããã詳现㯠PEP 567 ãåç §ããŠãã ããã
ããŒãžã§ã³ 3.8 ã§å€æŽ: Python 3.7 ãŸãã¯ãã以åã®ããŒãžã§ã³ã§ã¯ãããã©ã«ãã€ãã³ãã«ãŒãã®å®è£ ãå©çšããå Žåã«é å»¶æé delay ã1æ¥ãè¶ ããããšãã§ããŸããã§ããããã®åé¡ã¯ Python 3.8 ã§ä¿®æ£ãããŸããã
- loop.call_at(when, callback, *args, context=None)¶
絶察å€ã®æå» when (æŽæ°ãŸãã¯æµ®åå°æ°ç¹æ°) ã«ã³ãŒã«ããã¯ãåŒã³åºãããã«ã¹ã±ãžã¥ãŒã«ããŸãã
loop.time()ãšåãåç §æå»ã䜿çšããŸãããã®ã¡ãœããã®æ¯ãèãã¯
call_later()ãšåãã§ããasyncio.TimerHandleã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸãããã®ã€ã³ã¹ã¿ã³ã¹ã䜿ã£ãŠã¹ã±ãžã¥ãŒã«ããã³ãŒã«ããã¯ããã£ã³ã»ã«ããããšãã§ããŸããæ³šé
For performance, callbacks scheduled with
loop.call_at()may run up to one clock-resolution early (seetime.get_clock_info('monotonic').resolution).ããŒãžã§ã³ 3.7 ã§å€æŽ: ããŒã¯ãŒãåŒæ° context ã远å ãããŸããã詳现㯠PEP 567 ãåç §ããŠãã ããã
ããŒãžã§ã³ 3.8 ã§å€æŽ: Python 3.7 ãŸãã¯ãã以åã®ããŒãžã§ã³ã§ã¯ãããã©ã«ãã€ãã³ãã«ãŒãã®å®è£ ãå©çšããå Žåã«çŸåšã®æå»ãš when ãšã®å·®ã1æ¥ãè¶ ããããšãã§ããŸããã§ããããã®åé¡ã¯ Python 3.8 ã§ä¿®æ£ãããŸããã
- loop.time()¶
çŸåšã®æå»ã
floatå€ã§è¿ããŸããæå»ã¯ã€ãã³ãã«ãŒããå éšã§åç §ããŠããæå»ãæ»ããªãåèª¿ãªæèš (monotonic clock) ã«åŸããŸãã
泚é
ããŒãžã§ã³ 3.8 ã§å€æŽ: Python 3.7 ãŸãã¯ãã以åã®ããŒãžã§ã³ã§ã¯ãã¿ã€ã ã¢ãŠã (çžå¯Ÿå€ delay ãããã¯çµ¶å¯Ÿå€ when) ã¯1æ¥ãè¶ ããããšãã§ããŸããã§ããããã®åé¡ã¯ Python 3.8 ã§ä¿®æ£ãããŸããã
åè
颿° asyncio.sleep()ã
Creating futures and tasks¶
- loop.create_future()¶
ã€ãã³ãã«ãŒãã«æ¥ç¶ãã
asyncio.Futureãªããžã§ã¯ããçæããŸããasyncio ã§ãã¥ãŒãã£ãŒãªããžã§ã¯ããäœæããããã«æšå¥šãããæ¹æ³ã§ãããã®ã¡ãœããã«ããããµãŒãããŒãã£è£œã®ã€ãã³ãã«ãŒããFutures ã¯ã©ã¹ã®(ããã©ãŒãã³ã¹ãèšæž¬æ¹æ³ãåªãã) 代æ¿å®è£ ãæäŸããããšãå¯èœã«ããŸãã
Added in version 3.5.2.
- loop.create_task(coro, *, name=None, context=None, eager_start=None, **kwargs)¶
ã³ã«ãŒãã³ coro ã®å®è¡ãã¹ã±ãžã¥ãŒã«ããŸãã
Taskãªããžã§ã¯ããè¿ããŸãããµãŒãããŒãã£ã®ã€ãã³ãã«ãŒãã¯çžäºéçšã®ããã®èªèº«ã®
Taskã®ãµãã¯ã©ã¹ã䜿çšã§ããŸãããã®å Žåãçµæã¯Taskã®ãµãã¯ã©ã¹ã«ãªããŸããThe full function signature is largely the same as that of the
Taskconstructor (or factory) - all of the keyword arguments to this function are passed through to that interface.name åŒæ°ãæå®ãããå€ã
Noneã§ãªãå ŽåãTask.set_name()ã¡ãœããã«ãã name ãã¿ã¹ã¯ã®ååãšããŠèšå®ãããŸããçç¥å¯èœãªããŒã¯ãŒãåŒæ° context ã«ãã£ãŠãcoro ãå®è¡ããããã®ã«ã¹ã¿ã ã®
contextvars.Contextãæå®ã§ããŸããcontext ãçç¥ãããå ŽåãçŸåšã®ã³ã³ããã¹ãã®ã³ããŒãäœæãããŸããAn optional keyword-only eager_start argument allows specifying if the task should execute eagerly during the call to create_task, or be scheduled later. If eager_start is not passed the mode set by
loop.set_task_factory()will be used.ããŒãžã§ã³ 3.8 ã§å€æŽ: name ãã©ã¡ãŒã¿ã远å ããŸããã
ããŒãžã§ã³ 3.11 ã§å€æŽ: context ãã©ã¡ãŒã¿ã远å ããŸããã
ããŒãžã§ã³ 3.13.3 ã§å€æŽ: Added
kwargswhich passes on arbitrary extra parameters, includingnameandcontext.ããŒãžã§ã³ 3.13.4 ã§å€æŽ: Rolled back the change that passes on name and context (if it is None), while still passing on other arbitrary keyword arguments (to avoid breaking backwards compatibility with 3.13.3).
ããŒãžã§ã³ 3.14 ã§å€æŽ: All kwargs are now passed on. The eager_start parameter works with eager task factories.
- loop.set_task_factory(factory)¶
loop.create_task()ã䜿çšããã¿ã¹ã¯ãã¡ã¯ããªãŒãèšå®ããŸããIf factory is
Nonethe default task factory will be set. Otherwise, factory must be a callable with the signature matching(loop, coro, **kwargs), where loop is a reference to the active event loop, and coro is a coroutine object. The callable must pass on all kwargs, and return aasyncio.Task-compatible object.ããŒãžã§ã³ 3.13.3 ã§å€æŽ: Required that all kwargs are passed on to
asyncio.Task.ããŒãžã§ã³ 3.13.4 ã§å€æŽ: name is no longer passed to task factories. context is no longer passed to task factories if it is
None.ããŒãžã§ã³ 3.14 ã§å€æŽ: name and context are now unconditionally passed on to task factories again.
- loop.get_task_factory()¶
ã¿ã¹ã¯ãã¡ã¯ããªãè¿ããŸããããã©ã«ãã®ã¿ã¹ã¯ãã¡ã¯ããªã䜿çšäžã®å Žåã¯
Noneãè¿ããŸãã
ãããã¯ãŒã¯æ¥ç¶ã®ç¢ºç«Â¶
- async loop.create_connection(protocol_factory, host=None, port=None, *, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None, happy_eyeballs_delay=None, interleave=None, all_errors=False)¶
host ãš port ã§æå®ãããã¢ãã¬ã¹ãšã®ã¹ããªãŒãã³ã°ãã©ã³ã¹ããŒãæ¥ç¶ããªãŒãã³ããŸãã
ãœã±ãããã¡ããªãŒã¯ host (ãŸã㯠family åŒæ°ãäžããããå Žå㯠family) ã«äŸåãã
AF_INETãAF_INET6ã®ãããããæå®ããŸãããœã±ããã¿ã€ãã¯
SOCK_STREAMã«ãªããŸããprotocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
ãã®ã¡ãœããã¯ããã¯ã°ã©ãŠã³ãã§æ¥ç¶ã®ç¢ºç«ã詊ã¿ãŸããæåããå Žåãã¡ãœããã¯
(transport, protocol)ã®ãã¢ãè¿ããŸããæç³»åã§ã®äžå±€åŠçã®æŠèŠã¯ä»¥äžã®ãšããã§ã:
æ¥ç¶ã確ç«ãããã®æ¥ç¶ã«å¯Ÿãã ãã©ã³ã¹ããŒã ãçæãããŸãã
protocol_factory ãåŒæ°ãªãã§åŒã³åºããããã¡ã¯ããªã ãããã³ã« ã€ã³ã¹ã¿ã³ã¹ãè¿ãããèŠæ±ããŸãã
ãããã³ã«ã€ã³ã¹ã¿ã³ã¹ã
connection_made()ã¡ãœãããåŒã³åºãããšã«ããããã©ã³ã¹ããŒããšçŽä»ããããŸããæåãããš
(transport, protocol)ã¿ãã«ãè¿ãããŸãã
äœæããããã©ã³ã¹ããŒãã¯å®è£ äŸåã®åæ¹åã¹ããªãŒã ã§ãã
ãã®ä»ã®åŒæ°:
ssl: åœå€ä»¥å€ãäžããããå ŽåãSSL/TLS ãã©ã³ã¹ããŒããäœæãããŸã (ããã©ã«ãã§ã¯æå·åãªãã® TCP ãã©ã³ã¹ããŒããäœæãããŸã)ã ssl ã
ssl.SSLContextãªããžã§ã¯ãã®å Žåããã®ã³ã³ããã¹ãããã©ã³ã¹ããŒããäœæããããã«äœ¿çšãããŸã; ssl ãTrueã®å Žåãssl.create_default_context()ãè¿ãããã©ã«ãã®ã³ã³ããã¹ãã䜿ãããŸããserver_hostname ã¯å¯Ÿè±¡ãµãŒããŒã®èšŒææžãšã®äžèŽã確èªããããã®ãã¹ãåãèšå®ãŸãã¯äžæžãããŸãããã®åŒæ°ã¯ ssl ã
Noneã§ãªãå Žåã®ã¿èšå®ãã¹ãã§ããããã©ã«ãã§ã¯ host ã«æå®ãããµãŒããŒåã䜿çšãããŸãã host ãç©ºã®æååã®å Žåã®ããã©ã«ãå€ã¯èšå®ãããŠããŸããããã®å Žåã server_hostname ãå¿ ãæå®ããŠãã ããã server_hostname ãç©ºã®æååã®å Žåã¯ããã¹ãåã®äžèŽç¢ºèªã¯è¡ãããŸãã (ããã¯æ·±å»ãªã»ãã¥ãªãã£ãªã¹ã¯ã§ãããäžéè æ»æãåããå¯èœæ§ããããŸã)ãfamily, proto, flags ã¯ä»»æã®ã¢ãã¬ã¹ãã¡ããªã§ãããhost 解決ã®ããã® getaddrinfo() çµç±ã§æž¡ããããããã³ã«ããã³ãã©ã°ã«ãªããŸãããã®ãªãã·ã§ã³ãäžããããå Žåããããã¯ãã¹ãŠ
socketã¢ãžã¥ãŒã«å®æ°ã«åŸã£ãæŽæ°ã§ãªããã°ãªããŸãããhappy_eyeballs_delay ãèšå®ããããšããã®æ¥ç¶ã«å¯Ÿã㊠Happy Eyeballs ãæå¹åãããŸããèšå®ããå€ã¯æµ®åå°æ°ç¹æ°ã§ãããæ¬¡ã®æ¥ç¶è©Šè¡ãéå§ããåã«ãçŸåšã®æ¥ç¶è©Šè¡ãå®äºããã®ãåŸ ã€æéãç§åäœã§è¡šçŸããŸãããã®å€ã¯ RFC 8305 ã§å®çŸ©ãããŠãã "æ¥ç¶è©Šè¡é å»¶" ã«çžåœããŸããRFC ã§æšå¥šãããŠããå®çšçãªããã©ã«ãå€ã¯
0.25(250 ããªç§) ã§ããinterleave ã¯ãã¹ãåãè€æ°ã® IP ã¢ãã¬ã¹ã«åå解決ãããå Žåã®ã¢ãã¬ã¹ã®äžŠã¹æ¿ããå¶åŸ¡ããŸãã
0ãŸãã¯æªæå®ã®å ŽåäžŠã¹æ¿ãã¯è¡ããããgetaddrinfo()ãè¿ãé çªã«ãããã£ãŠã¢ãã¬ã¹ãžã®æ¥ç¶ã詊è¡ããŸããæ£ã®æŽæ°ãæå®ããããšãã¢ãã¬ã¹ã¯ã¢ãã¬ã¹ãã¡ããªã«å¿ããŠã€ã³ã¿ãŒãªãŒããããŸãããã®ãšããäžããããæŽæ°ã¯ RFC 8305 ã§å®çŸ©ããã "æåã®ã¢ãã¬ã¹ãã¡ããªã«ãŠã³ã (First Address Family Count)" ãšããŠè§£éãããŸããããã©ã«ãå€ã¯ã happy_eyeballs_delay ãæå®ãããªãå Žåã¯0ã§ãããæå®ãããå Žåã¯1ã§ããsock ãäžããå Žåããã©ã³ã¹ããŒãã«äœ¿çšããããæ¢åã®ããã€æ¥ç¶æžã®
socket.socketãªããžã§ã¯ããæå®ããŸããsock ãæå®ããå Žåãhostã portã familyã protoã flagsã happy_eyeballs_delayã interleave ããã³ local_addr ã®ããããæå®ããŠã¯ãããŸãããæ³šé
The sock argument transfers ownership of the socket to the transport created. To close the socket, call the transport's
close()method.local_addr ãäžããå Žåããœã±ãããããŒã«ã«ã«ãã€ã³ãããããã«äœ¿çšãã
(local_host, local_port)ã¿ãã«ãæå®ããŸãã local_host ãš local_port ã¯ã host ããã³ port ãšåããgetaddrinfo()ã䜿ã£ãŠã«ãã¯ã¢ãããããŸããssl_handshake_timeout 㯠TLS ãã³ãã·ã§ã€ã¯ãå®äºãããŸã§ã® (TLS æ¥ç¶ã®ããã®) åŸ ã¡æéãç§åäœã§æå®ããŸããæå®ããåŸ ã¡æéãè¶ ãããšæ¥ç¶ã¯äžæããŸãã
Noneãäžããããå Žåã¯ããã©ã«ãå€60.0ã䜿ãããŸããssl_shutdown_timeout is the time in seconds to wait for the SSL shutdown to complete before aborting the connection.
30.0seconds ifNone(default).all_errors determines what exceptions are raised when a connection cannot be created. By default, only a single
Exceptionis raised: the first exception if there is only one or all errors have same message, or a singleOSErrorwith the error messages combined. Whenall_errorsisTrue, anExceptionGroupwill be raised containing all exceptions (even if there is only one).
ããŒãžã§ã³ 3.5 ã§å€æŽ:
ProactorEventLoopã«ãã㊠SSL/TLS ã®ãµããŒãã远å ãããŸãããããŒãžã§ã³ 3.6 ã§å€æŽ: å šãŠã® TCP æ¥ç¶ã«å¯ŸããŠããã©ã«ãã§ãœã±ãããªãã·ã§ã³ socket.TCP_NODELAY ãèšå®ãããããã«ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ssl_handshake_timeout åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.8 ã§å€æŽ: happy_eyeballs_delay ãš interleave ã远å ãããŸããã
Happy Eyeballs Algorithm: Success with Dual-Stack Hosts. When a server's IPv4 path and protocol are working, but the server's IPv6 path and protocol are not working, a dual-stack client application experiences significant connection delay compared to an IPv4-only client. This is undesirable because it causes the dual-stack client to have a worse user experience. This document specifies requirements for algorithms that reduce this user-visible delay and provides an algorithm.
詳ããã¯å³èšãåç §ããŠãã ãã: https://datatracker.ietf.org/doc/html/rfc6555
ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.12 ã§å€æŽ: all_errors ã远å ãããŸãã
ããŒãžã§ã³ 3.14.8 ã§å€æŽ: Raises a
ValueErrorifssl.check_hostnameisTrueandserver_hostnameis not supplied.åè
open_connection()颿°ã¯é«æ°Žæºã®ä»£æ¿ API ã§ãããã®é¢æ°ã¯(StreamReader,StreamWriter) ã®ãã¢ãè¿ãã async/await ã³ãŒãããçŽæ¥äœ¿ãããšãã§ããŸãã
- async loop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, *, family=0, proto=0, flags=0, reuse_port=None, allow_broadcast=None, sock=None)¶
ããŒã¿ã°ã©ã æ¥ç¶ (UDP) ãçæããŸãã
ãœã±ãããã¡ããªãŒã¯ host (ãŸã㯠family åŒæ°ãäžããããå Žå㯠family) ã«äŸåãã
AF_INETãAF_INET6ãAF_UNIXã®ãããããæå®ããŸãããœã±ããã¿ã€ãã¯
SOCK_DGRAMã«ãªããŸããprotocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
æåãããš
(transport, protocol)ã¿ãã«ãè¿ãããŸãããã®ä»ã®åŒæ°:
local_addr ãæå®ãããå Žåããœã±ãããããŒã«ã«ã«ãã€ã³ãããããã®
(local_host, local_port)ã®ã¿ãã«ãæå®ããŸãã local_host ãš local_port ã¯getaddrinfo()ã¡ãœããã䜿çšããŠæ€çŽ¢ãããŸããæ³šé
On Windows, when using the proactor event loop with
local_addr=None, anOSErrorwitherrno.WSAEINVALwill be raised when running it.remote_addr ãæå®ãããå Žåã
(remote_host, remote_por)ã®ã¿ãã«ã§ããœã±ããããªã¢ãŒãã¢ãã¬ã¹ã«æçžããããã«äœ¿çšãããŸããremote_host ãš remote_port ã¯getaddrinfo()ã䜿çšããŠæ€çŽ¢ãããŸããfamily, proto, flags ã¯ä»»æã®ã¢ãã¬ã¹ãã¡ããªã§ãããããã®ãã¡ããªããããã³ã«ããã©ã°ã¯ãhost 解決ã®ãã
getaddrinfo()çµç±ã§ãªãã·ã§ã³ã§æž¡ãããŸãããããã®ãªãã·ã§ã³ãæå®ããå Žåããã¹ãŠsocketã¢ãžã¥ãŒã«å®æ°ã«åŸã£ãæŽæ°ã§ãªããã°ãªããŸãããreuse_port ã¯ãåãããŒãã«ãã€ã³ããããæ¢åã®ç«¯ç¹ãã¹ãŠããã®ãã©ã°ãèšå®ããŠçæãããŠããå Žåã«éãããã®ç«¯ç¹ãæ¢åã®ç«¯ç¹ãšåãããŒãã«ãã€ã³ãããããšãèš±å¯ãããããã«ãŒãã«ã«æç€ºããŸãïŒèš³èš»: ãœã±ããã®ãªãã·ã§ã³ SO_REUSEPORT ã䜿çšããŸãïŒããã®ãªãã·ã§ã³ã¯ãWindows ãããã€ãã® UNIX ã·ã¹ãã ã§ã¯ãµããŒããããŠããŸãããsocket.SO_REUSEPORT 宿°ãå®çŸ©ãããŠããªããã°ããã®æ©èœã¯ãµããŒããããŸããã
allow_broadcast ã¯ãã«ãŒãã«ã«ããã®ãšã³ããã€ã³ãããããŒããã£ã¹ãã¢ãã¬ã¹ã«ã¡ãã»ãŒãžãéä¿¡ããããšãèš±å¯ããããã«æç€ºããŸãã
ãªãã·ã§ã³ã® sock ãæå®ããããšã§ãæ¢åã®ããã§ã«æ¥ç¶ãããŠãã
socket.socketããã©ã³ã¹ããŒãã§äœ¿çšããããšãã§ããŸãããã®ãªãã·ã§ã³ã䜿çšããå Žåãlocal_addr ãš remote_addr ã¯çç¥ããŠãã ãã (Noneã§ãªããã°ãªããŸãã)ãæ³šé
The sock argument transfers ownership of the socket to the transport created. To close the socket, call the transport's
close()method.
UDP echo ã¯ã©ã€ã¢ã³ããããã³ã« ããã³ UDP echo ãµãŒããŒãããã³ã« ã®äŸãåç §ããŠãã ããã
ããŒãžã§ã³ 3.4.4 ã§å€æŽ: family, proto, flags, reuse_address, reuse_port, allow_broadcast, sock ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.8 ã§å€æŽ: Windows ãµããŒãã远å ãããŸããã
ããŒãžã§ã³ 3.8.1 ã§å€æŽ: socket.SO_REUSEADDR ã®å©çšã UDP ã«å¯ŸããŠé倧ãªã»ãã¥ãªãã£äžã®æžå¿µãããããããã reuse_address ãã©ã¡ãŒã¿ã¯ãµããŒããããªããªããŸãããæç€ºçã«
reuse_address=Trueãèšå®ãããšäŸå€ãéåºããŸããSO_REUSEADDRã䜿ã£ãŠãåäžã® UDP ãœã±ããã¢ãã¬ã¹ã«å¯ŸããŠè€æ°ã®ããã»ã¹ãç°ãªã UID ã§ãœã±ãããå²ãåœãŠãŠããå Žåãåä¿¡ãã±ããã¯è€æ°ã®ãœã±ããéã«ã©ã³ãã ã«åæ£ããå¯èœæ§ããããŸãããµããŒããããŠãããã©ãããã©ãŒã ã§ã¯ã reuse_port ãåæ§ã®æ©èœã«å¯Ÿãã代çšåãšããŠå©çšã§ããŸãã reuse_port ã¯ä»£æ¿æ©èœãšã㊠socket.SO_REUSEPORT ã䜿ã£ãŠãããè€æ°ã®ããã»ã¹ãç°ãªã UID ã§åäžã®ãœã±ããã«å¯ŸããŠå²ãåœãŠãããã®ãæç¢ºã«çŠæ¢ããŸãã
ããŒãžã§ã³ 3.11 ã§å€æŽ: The reuse_address parameter, disabled since Python 3.8.1, 3.7.6 and 3.6.10, has been entirely removed.
- async loop.create_unix_connection(protocol_factory, path=None, *, ssl=None, sock=None, server_hostname=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None)¶
Unix æ¥ç¶ãçæããŸãã
ãœã±ãããã¡ããªãŒã¯
AF_UNIXã«ãªããŸã; ãŸãããœã±ããã¿ã€ãã¯SOCK_STREAMã«ãªããŸããæåãããš
(transport, protocol)ã¿ãã«ãè¿ãããŸããpath 㯠Unix ãã¡ã€ã³ãœã±ããåã§ã sock ãã©ã¡ãŒã¿ãæå®ãããªãå Žåã¯å¿ é ã§ãã æœè±¡ Unix ãœã±ããã
strãbytesã andPath圢åŒã§ã®ãã¹ããµããŒããããŠããŸãããã®ã¡ãœããã®åŒæ°ã«ã€ããŠã®è©³çްã¯
loop.create_connection()ã¡ãœããã®ããã¥ã¡ã³ããåç §ããŠãã ãããAvailability: Unix.
ããŒãžã§ã³ 3.7 ã§å€æŽ: Added the ssl_handshake_timeout parameter. The path parameter can now be a path-like object.
ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
ãããã¯ãŒã¯ãµãŒãã®çæÂ¶
- async loop.create_server(protocol_factory, host=None, port=None, *, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None, keep_alive=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None, start_serving=True)¶
Create a TCP server (socket type
SOCK_STREAM) listening on port of the host address.Serverãªããžã§ã¯ããè¿ããŸããåŒæ°:
protocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
host ãã©ã¡ãŒã¿ã¯ããã€ãã®æ¹æ³ã§æå®ããããšãã§ãããã®å€ã«ãã£ãŠãµãŒããŒãã©ãããªãã¹ã³ããããæ±ºãŸããŸãã
host ãæååã®å Žåã TCP ãµãŒããŒã¯ host ã§æå®ããåäžã®ãããã¯ãŒã¯ã€ã³ã¿ãŒãã§ãŒã¹ã«æçžãããŸãã
host ãæååã®ã·ãŒã±ã³ã¹ã§ããå Žåã TCP ãµãŒããŒã¯ãã®ã·ãŒã±ã³ã¹ã§æå®ãããå šãŠã®ãããã¯ãŒã¯ã€ã³ã¿ãŒãã§ãŒã¹ã«æçžãããŸãã
host ãç©ºã®æååã
Noneã®å Žåããã¹ãŠã®ã€ã³ã¿ãŒãã§ãŒã¹ãæ³å®ãããè€åçãªãœã±ããã®ãªã¹ã (éåžžã¯äžã€ã IPv4ãããäžã€ã IPv6) ãè¿ãããŸãã
The port parameter can be set to specify which port the server should listen on. If
0orNone(the default), a random unused port will be selected (note that if host resolves to multiple network interfaces, a different random port will be selected for each interface).family can be set to either
socket.AF_INETorAF_INET6to force the socket to use IPv4 or IPv6. If not set, the family will be determined from host name (defaults toAF_UNSPEC).flags ã¯
getaddrinfo()ã®ããã®ããããã¹ã¯ã«ãªããŸãããµãŒããŒã§æ¢åã®ãœã±ãããªããžã§ã¯ãã䜿çšããããã«ããªãã·ã§ã³ã®åŒæ° sock ã«ãœã±ãããªããžã§ã¯ããèšå®ããããšãã§ããŸããæå®ããå Žåã host ãš port ãæå®ããŠã¯ãããŸããã
泚é
The sock argument transfers ownership of the socket to the server created. To close the socket, call the server's
close()method.backlog ã¯
listen()ã«æž¡ãããããã¥ãŒã«å ¥ãã³ãã¯ã·ã§ã³ã®æå€§æ°ã«ãªããŸã (ããã©ã«ã㯠100)ã確ç«ããæ¥ç¶ã®äžã§ TLS ãæå¹åããããã«ã ssl ã«
SSLContextã®ã€ã³ã¹ã¿ã³ã¹ãæå®ããããšãã§ããŸããreuse_address ã¯ã
TIME_WAITç¶æ ã«ããããŒã«ã«ãœã±ãããããã®ç¶æ ãèªç¶ã«ã¿ã€ã ã¢ãŠãããã®ãåŸ ã€ããšãªãåå©çšããããã«ãŒãã«ã«æç€ºããŸãïŒèš³èš»: ãœã±ããã®ãªãã·ã§ã³ SO_REUSEADDR ã䜿çšããŸãïŒãæå®ããªãå ŽåãUNIX ã§ã¯èªåçã«Trueãèšå®ãããŸããreuse_port ã¯ãåãããŒãã«ãã€ã³ããããæ¢åã®ç«¯ç¹ãã¹ãŠããã®ãã©ã°ãèšå®ããŠçæãããŠããå Žåã«éãããã®ç«¯ç¹ãæ¢åã®ç«¯ç¹ãšåãããŒãã«ãã€ã³ãããããšãèš±å¯ãããããã«ãŒãã«ã«æç€ºããŸãïŒèš³èš»: ãœã±ããã®ãªãã·ã§ã³ SO_REUSEPORT ã䜿çšããŸãïŒããã®ãªãã·ã§ã³ã¯ãWindows ã§ã¯ãµããŒããããŠããŸããã
keep_alive set to
Truekeeps connections active by enabling the periodic transmission of messages.
ããŒãžã§ã³ 3.13 ã§å€æŽ: Added the keep_alive parameter.
ssl_handshake_timeout 㯠TLS ãã³ãã·ã§ã€ã¯ãå®äºãããŸã§ã® (TLS ãµãŒããŒã®ããã®) åŸ ã¡æéãç§åäœã§æå®ããŸããæå®ããåŸ ã¡æéãè¶ ãããšæ¥ç¶ã¯äžæããŸãã
Noneãäžããããå Žåã¯ããã©ã«ãå€60.0ã䜿ãããŸããssl_shutdown_timeout is the time in seconds to wait for the SSL shutdown to complete before aborting the connection.
30.0seconds ifNone(default).start_serving ã
Trueã«èšå®ãããå Žå (ãããããã©ã«ãã§ã)ã çæããããµãŒããŒã¯å³åº§ã«æ¥ç¶ã®åãä»ããéå§ããŸããFalseãæå®ãããå ŽåããŠãŒã¶ãŒã¯æ¥ç¶ã®åãä»ããéå§ããããã«Server.start_serving()ãŸãã¯Server.serve_forever()ãåŸ ã¡åã (await) ãå¿ èŠããããŸãã
ããŒãžã§ã³ 3.5 ã§å€æŽ:
ProactorEventLoopã«ãã㊠SSL/TLS ã®ãµããŒãã远å ãããŸãããããŒãžã§ã³ 3.5.1 ã§å€æŽ: host ãã©ã¡ãŒã¿ã«æååã®ã·ãŒã±ã³ã¹ãæå®ã§ããããã«ãªããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: Added ssl_handshake_timeout and start_serving parameters. The socket option socket.TCP_NODELAY is set by default for all TCP connections.
ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
åè
start_server()颿°ã¯é«æ°Žæºã®ä»£æ¿ API ã§ãããã®é¢æ°ã¯StreamReaderãšStreamWriterã®ãã¢ãè¿ããasync/await ã³ãŒããã䜿ãããšãã§ããŸãã
- async loop.create_unix_server(protocol_factory, path=None, *, sock=None, backlog=100, ssl=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None, start_serving=True, cleanup_socket=True)¶
Similar to
loop.create_server()but works with theAF_UNIXsocket family.path 㯠Unix ãã¡ã€ã³ãœã±ããåã§ã sock ãã©ã¡ãŒã¿ãæå®ãããªãå Žåã¯å¿ é ã§ãã æœè±¡ Unix ãœã±ããã
strãbytesã andPath圢åŒã§ã®ãã¹ããµããŒããããŠããŸããIf cleanup_socket is true then the Unix socket will automatically be removed from the filesystem when the server is closed, unless the socket has been replaced after the server has been created.
ãã®ã¡ãœããã®åŒæ°ã«ã€ããŠã®è©³çްã¯
loop.create_server()ã¡ãœããã®ããã¥ã¡ã³ããåç §ããŠãã ãããAvailability: Unix.
ããŒãžã§ã³ 3.7 ã§å€æŽ: Added the ssl_handshake_timeout and start_serving parameters. The path parameter can now be a
Pathobject.ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.13 ã§å€æŽ: Added the cleanup_socket parameter.
- async loop.connect_accepted_socket(protocol_factory, sock, *, ssl=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None)¶
ãã§ã«ç¢ºç«ããæ¥ç¶ã transport ãš protocol ã®ãã¢ã§ã©ããããŸãã
ãã®ã¡ãœãã㯠asyncio ã®ç¯å²å€ã§ç¢ºç«ãããæ¥ç¶ã䜿ããµãŒããŒã«å¯ŸããŠã䜿ããŸããããã®å Žåã§ãæ¥ç¶ã¯ asyncio ã䜿ã£ãŠåŠçãããŸãã
åŒæ°:
protocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
sock ã¯
socket.acceptã¡ãœãããè¿ãæ¢åã®ãœã±ãããªããžã§ã¯ãã§ããæ³šé
The sock argument transfers ownership of the socket to the transport created. To close the socket, call the transport's
close()method.ssl ã«ã¯
SSLContextãæå®ã§ããŸããæå®ãããšãåãä»ããã³ãã¯ã·ã§ã³äžã§ã® SSL ãæå¹ã«ããŸããssl_handshake_timeout 㯠SSL ãã³ãã·ã§ã€ã¯ãå®äºãããŸã§ã® (SSL æ¥ç¶ã®ããã®) åŸ ã¡æéãç§åäœã§æå®ããŸãã
Noneãäžããããå Žåã¯ããã©ã«ãå€60.0ã䜿ãããŸããssl_shutdown_timeout is the time in seconds to wait for the SSL shutdown to complete before aborting the connection.
30.0seconds ifNone(default).
(transport, protocol)ã®ãã¢ãè¿ããŸããAdded in version 3.5.3.
ããŒãžã§ã³ 3.7 ã§å€æŽ: ssl_handshake_timeout åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
ãã¡ã€ã«ã®è»¢é¶
- async loop.sendfile(transport, file, offset=0, count=None, *, fallback=True)¶
transport ãéã㊠file ãéä¿¡ããŸããéä¿¡ããããŒã¿ã®ç·ãã€ãæ°ãè¿ããŸãã
ãã®ã¡ãœããã¯ãããå©çšå¯èœã§ããã°é«æ§èœãª
os.sendfile()ãå©çšããŸããfile ã¯ãã€ããªã¢ãŒãã§ãªãŒãã³ãããéåžžã®ãã¡ã€ã«ãªããžã§ã¯ãã§ãªããã°ãªããŸããã
offset ã¯ãã¡ã€ã«ã®èªã¿èŸŒã¿éå§äœçœ®ãæå®ããŸãã count ãæå®ãããå Žåããã¡ã€ã«ã® EOF ãŸã§ãã¡ã€ã«ãéä¿¡ãã代ããã«ã count ã§æå®ãããç·ãã€ãæ°ã®åã ãéä¿¡ããŸãããã¡ã€ã«ãªããžã§ã¯ããæã瀺ãäœçœ®ã¯ãã¡ãœããããšã©ãŒãéåºããå Žåã§ãæŽæ°ãããŸãããã®å Žåå®éã«éä¿¡ããããã€ãæ°ã¯
file.tell()ã¡ãœããã§ååŸããããšãã§ããŸããfallback ã
Trueã«æå®ããããšã§ã asyncio ããã©ãããã©ãŒã ã sendfile ã·ã¹ãã ã³ãŒã«ããµããŒãããŠããªãå Žå (ããšãã° Windows ã Unix ã® SSL ãœã±ãããªã©) ã«å¥ã®æ¹æ³ã§ãã¡ã€ã«ã®èªã¿èŸŒã¿ãšéä¿¡ãè¡ãããã«ããããšãã§ããŸããã·ã¹ãã ã sendfile ã·ã¹ãã ã³ãŒã«ããµããŒãããŠããããã〠fallback ã
Falseã®å ŽåãSendfileNotAvailableErroräŸå€ãéåºããŸããAdded in version 3.7.
TLS upgrade¶
- async loop.start_tls(transport, protocol, sslcontext, *, server_side=False, server_hostname=None, ssl_handshake_timeout=None, ssl_shutdown_timeout=None)¶
æ¢åã®ãã©ã³ã¹ããŒãããŒã¹ã®æ¥ç¶ã TLS ã«ã¢ããã°ã¬ãŒãããŸãã
Create a TLS coder/decoder instance and insert it between the transport and the protocol. The coder/decoder implements both transport-facing protocol and protocol-facing transport.
Return the created two-interface instance. After await, the protocol must stop using the original transport and communicate with the returned object only because the coder caches protocol-side data and sporadically exchanges extra TLS session packets with transport.
In some situations (e.g. when the passed transport is already closing) this may return
None.åŒæ°:
transport ãš protocol ã«ã¯ã
create_server()ãcreate_connection()ãè¿ããã®ãšåçã®ã€ã³ã¹ã¿ã³ã¹ãæå®ããŸããsslcontext: æ§ææžã¿ã®
SSLContextã€ã³ã¹ã¿ã³ã¹ã§ãã(
create_server()ã§çæããããããª) ãµãŒããŒãµã€ãã®æ¥ç¶ãã¢ããã°ã¬ãŒãããå Žå㯠server_side ã«Trueãæž¡ããŸããserver_hostname: 察象ã®ãµãŒããŒã®èšŒææžãšã®ç §åã«äœ¿ããããã¹ãåãèšå®ãŸãã¯äžæžãããŸãã
ssl_handshake_timeout 㯠TLS ãã³ãã·ã§ã€ã¯ãå®äºãããŸã§ã® (TLS æ¥ç¶ã®ããã®) åŸ ã¡æéãç§åäœã§æå®ããŸããæå®ããåŸ ã¡æéãè¶ ãããšæ¥ç¶ã¯äžæããŸãã
Noneãäžããããå Žåã¯ããã©ã«ãå€60.0ã䜿ãããŸããssl_shutdown_timeout is the time in seconds to wait for the SSL shutdown to complete before aborting the connection.
30.0seconds ifNone(default).
Added in version 3.7.
ããŒãžã§ã³ 3.11 ã§å€æŽ: ssl_shutdown_timeout ãã©ã¡ãŒã¿ã远å ãããŸããã
ãã¡ã€ã«èšè¿°åã®ç£èŠÂ¶
- loop.add_reader(fd, callback, *args)¶
ãã¡ã€ã«èšè¿°å fd ã«å¯Ÿããèªã¿èŸŒã¿ãå¯èœãã©ããã®ç£èŠãéå§ãã fd ãèªã¿èŸŒã¿å¯èœã«ãªããšãæå®ããåŒæ°ã§ã³ãŒã«ãã㯠callback ãåŒã³åºããŸãã
Any preexisting callback registered for fd is cancelled and replaced by callback.
- loop.remove_reader(fd)¶
Stop monitoring the fd file descriptor for read availability. Returns
Trueif fd was previously being monitored for reads.
- loop.add_writer(fd, callback, *args)¶
Start monitoring the fd file descriptor for write availability and invoke callback with the specified arguments args once fd is available for writing.
Any preexisting callback registered for fd is cancelled and replaced by callback.
ã³ãŒã«ãã㯠callback ã« ããŒã¯ãŒãåŒæ°ãæž¡ã å Žåã¯
functools.partial()ã䜿ã£ãŠãã ããã
- loop.remove_writer(fd)¶
Stop monitoring the fd file descriptor for write availability. Returns
Trueif fd was previously being monitored for writes.
ãããã®ã¡ãœããã«å¯Ÿããå¶éäºé ã«ã€ããŠã¯ ãã©ãããã©ãŒã ã®ãµããŒãç¶æ³ ç¯ãåç §ããŠãã ããã
ãœã±ãããªããžã§ã¯ããšçŽæ¥ãããšããã¶
äžè¬ã«ã loop.create_connection() ã loop.create_server() ã®ãããªãã©ã³ã¹ããŒãããŒã¹ã® API ã䜿ã£ããããã³ã«ã®å®è£
ã¯ãœã±ãããšçŽæ¥ããåãããå®è£
ã«æ¯ã¹ãŠé«éã§ããããããªãããããã©ãŒãã³ã¹ãéèŠã§ãªããçŽæ¥ socket ãªããžã§ã¯ããšãããšãããæ¹ã䟿å©ãªãŠãŒã¹ã±ãŒã¹ãããã€ããããŸãã
- async loop.sock_recv(sock, nbytes)¶
nbytes ã§æå®ãããã€ãæ°ãŸã§ã®ããŒã¿ããœã±ãã sock ããåä¿¡ããŸãã ãã®ã¡ãœããã¯
socket.recv()ã®éåæçã§ããåä¿¡ããããŒã¿ããã€ããªããžã§ã¯ããšããŠè¿ããŸãã
sock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ãã®ã¡ãœããã¯åžžã«ã³ã«ãŒãã³ã¡ãœãããšããŠããã¥ã¡ã³ãã«èšèŒãããŠããŸãããã Python 3.7 以åã®ãªãªãŒã¹ã§ã¯
Futureãªããžã§ã¯ããè¿ããŠããŸããã Python 3.7 ããã¯async defã¡ãœããã«ãªããŸããã
- async loop.sock_recv_into(sock, buf)¶
ãœã±ãã sock ããããŒã¿ãåä¿¡ããŠãããã¡ buf ã«æ ŒçŽããŸããããããã³ã°ã³ãŒãã®
socket.recv_into()ã¡ãœãããã¢ãã«ãšããŠããŸãããããã¡ã«æžã蟌ãã ããŒã¿ã®ãã€ãæ°ãè¿ããŸãã
sock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
Added in version 3.7.
- async loop.sock_recvfrom(sock, bufsize)¶
Receive a datagram of up to bufsize from sock. Asynchronous version of
socket.recvfrom().Return a tuple of (received data, remote address).
sock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
Added in version 3.11.
- async loop.sock_recvfrom_into(sock, buf, nbytes=0)¶
Receive a datagram of up to nbytes from sock into buf. Asynchronous version of
socket.recvfrom_into().Return a tuple of (number of bytes received, remote address).
sock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
Added in version 3.11.
- async loop.sock_sendall(sock, data)¶
ããŒã¿ data ããœã±ãã sock ã«éä¿¡ããŸãã
socket.sendall()ã¡ãœããã®éåæçã§ãããã®ã¡ãœãã㯠data ããã¹ãŠéä¿¡ãçµãããããŸãã¯ãšã©ãŒãèµ·ãããŸã§ããŒã¿ããœã±ããã«éä¿¡ãç¶ããŸããéä¿¡ã«æåããå Žå
Noneãè¿ããŸãããšã©ãŒã®å Žåã¯äŸå€ãéåºãããŸãããšã©ãŒãšãªã£ãå Žåãæ¥ç¶ã®åä¿¡åŽã§æ£ããåŠçãããããŒã¿ã®ç·éãç¹å®ããæ¹æ³ã¯ãããŸãããsock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ãã®ã¡ãœããã¯åžžã«ã³ã«ãŒãã³ã¡ãœãããšããŠããã¥ã¡ã³ãã«èšèŒãããŠããŸãããã Python 3.7 以åã®ãªãªãŒã¹ã§ã¯
Futureãªããžã§ã¯ããè¿ããŠããŸããã Python 3.7 ããã¯async defã¡ãœããã«ãªããŸããã
- async loop.sock_sendto(sock, data, address)¶
Send a datagram from sock to address. Asynchronous version of
socket.sendto().Return the number of bytes sent.
sock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
Added in version 3.11.
- async loop.sock_connect(sock, address)¶
ãœã±ãã sock ãã¢ãã¬ã¹ address ã®ãªã¢ãŒããœã±ããã«æ¥ç¶ããŸãã
socket.connect()ã®éåæçã§ããsock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
With
SelectorEventLoop, address does not need to be resolved: forAF_INETandAF_INET6sockets,sock_connectfirst checks whether address is already resolved by callingsocket.inet_pton(), and usesloop.getaddrinfo()to resolve it if it is not.ProactorEventLoop, the default event loop on Windows, does not resolve address. The host must already be a numeric IP address; passing a host name raisesOSError. Resolve the address withloop.getaddrinfo()first, or useloop.create_connection(), which resolves the address on every platform.ããŒãžã§ã³ 3.5.2 ã§å€æŽ: With
SelectorEventLoop,addressno longer needs to be resolved.åè
loop.create_connection()ããã³asyncio.open_connection()ã
- async loop.sock_accept(sock)¶
æ¥ç¶ãåãä»ããŸããããããã³ã°ã³ãŒã«ã®
socket.accept()ã¡ãœãããã¢ãã«ãšããŠããŸãããœã±ããã¯ã¢ãã¬ã¹ã«æçžæžã¿ã§ãæ¥ç¶ã listen äžã§ããå¿ èŠããããŸããæ»ãå€ã¯
(conn, address)ã®ãã¢ã§ãconn ã¯æ¥ç¶ãéããŠããŒã¿ã®éåä¿¡ãè¡ãããã® æ°ãã ãœã±ãããªããžã§ã¯ããaddress ã¯æ¥ç¶å ã®ç«¯ç¹ã§ãœã±ããã«æçžãããŠããã¢ãã¬ã¹ã瀺ããŸããsock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ãã®ã¡ãœããã¯åžžã«ã³ã«ãŒãã³ã¡ãœãããšããŠããã¥ã¡ã³ãã«èšèŒãããŠããŸãããã Python 3.7 以åã®ãªãªãŒã¹ã§ã¯
Futureãªããžã§ã¯ããè¿ããŠããŸããã Python 3.7 ããã¯async defã¡ãœããã«ãªããŸãããåè
loop.create_server()ããã³start_server()ã
- async loop.sock_sendfile(sock, file, offset=0, count=None, *, fallback=True)¶
ãã¡ã€ã«ãéä¿¡ããŸããå©çšå¯èœãªã髿§èœãª
os.sendfileã䜿ããŸããéä¿¡ããããŒã¿ã®ç·ãã€ãæ°ãè¿ããŸããsocket.sendfile()ã¡ãœããã®éåæçã§ããsock ã¯
socket.SOCK_STREAMã¿ã€ãã®ãã³ããããã³ã°ãªsocketã§ãªããã°ãªããŸãããfile ã¯ãã€ããªã¢ãŒãã§ãªãŒãã³ãããéåžžã®ãã¡ã€ã«ãªããžã§ã¯ãã§ãªããã°ãªããŸããã
offset ã¯ãã¡ã€ã«ã®èªã¿èŸŒã¿éå§äœçœ®ãæå®ããŸãã count ãæå®ãããå Žåããã¡ã€ã«ã® EOF ãŸã§ãã¡ã€ã«ãéä¿¡ãã代ããã«ã count ã§æå®ãããç·ãã€ãæ°ã®åã ãéä¿¡ããŸãããã¡ã€ã«ãªããžã§ã¯ããæã瀺ãäœçœ®ã¯ãã¡ãœããããšã©ãŒãéåºããå Žåã§ãæŽæ°ãããŸãããã®å Žåå®éã«éä¿¡ããããã€ãæ°ã¯
file.tell()ã¡ãœããã§ååŸããããšãã§ããŸããfallback ã
Trueã«èšå®ãããå Žåã ãã©ãããã©ãŒã ã sendfile ã·ã¹ãã ã³ãŒã«ããµããŒãããŠããªãå Žå (ããšãã° Windows ã Unix ã® SSL ãœã±ãããªã©) ã« asyncio ãå¥ã®æ¹æ³ã§ãã¡ã€ã«ã®èªã¿èŸŒã¿ãšéä¿¡ãè¡ãããã«ããããšãã§ããŸããã·ã¹ãã ã sendfile ã·ã¹ãã ã³ãŒã«ããµããŒãããŠããããã〠fallback ã
Falseã®å ŽåãSendfileNotAvailableErroräŸå€ãéåºããŸããsock ã¯ãã³ããããã³ã°ãœã±ããã§ãªããã°ãªããŸããã
Added in version 3.7.
DNS¶
- async loop.getaddrinfo(host, port, *, family=0, type=0, proto=0, flags=0)¶
socket.getaddrinfo()ã®éåæçã§ãã
- async loop.getnameinfo(sockaddr, flags=0)¶
socket.getnameinfo()ã®éåæçã§ãã
泚é
Both getaddrinfo and getnameinfo internally utilize their synchronous versions through the loop's default thread pool executor. When this executor is saturated, these methods may experience delays, which higher-level networking libraries may report as increased timeouts. To mitigate this, consider using a custom executor for other user tasks, or setting a default executor with a larger number of workers.
ããŒãžã§ã³ 3.7 ã§å€æŽ: getaddrinfo ãš getnameinfo ã®2ã€ã®ã¡ãœããã¯ãããããåžžã«ã³ã«ãŒãã³ã¡ãœãããšããŠããã¥ã¡ã³ãã«èšèŒãããŠããŸãããã Python 3.7 以åã®ãªãªãŒã¹ã§ã¯ãå®éã«ã¯ asyncio.Future ãªããžã§ã¯ããè¿ããŠããŸããã Python 3.7 ããã¯ã©ã¡ãã®ã¡ãœãããã³ã«ãŒãã³ã«ãªããŸããã
ãã€ããšãããšããã¶
- async loop.connect_read_pipe(protocol_factory, pipe)¶
ã€ãã³ãã«ãŒãã®èªã¿èŸŒã¿åŽçµç«¯ã« pipe ãç»é²ããŸãã
protocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
pipe is a file-like object. See Supported pipe objects for the objects supported as pipe.
(transport, protocol)ã®ãã¢ãè¿ããŸããããã§ transport ã¯ReadTransportã®ã€ã³ã¿ãŒãã§ãŒã¹ããµããŒããã protocol 㯠protocol_factory ãã¡ã¯ããªã§ã€ã³ã¹ã¿ã³ã¹åããããªããžã§ã¯ãã§ããSelectorEventLoopã€ãã³ãã«ãŒãã®å Žåãpipe ã¯éãããã¯ã¢ãŒãã«èšå®ãããŠããªããã°ãªããŸããã
- async loop.connect_write_pipe(protocol_factory, pipe)¶
pipe ã®æžã蟌ã¿åŽçµç«¯ãã€ãã³ãã«ãŒãã«ç»é²ããŸãã
protocol_factory 㯠asyncio ãããã³ã« ã®å®è£ ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããã
pipe is a file-like object. See Supported pipe objects for the objects supported as pipe.
(transport, protocol)ã®ãã¢ãè¿ããŸããããã§ transport ã¯WriteTransportã®ã€ã³ã¹ã¿ã³ã¹ã§ããã protocol 㯠protocol_factory ãã¡ã¯ããªã§ã€ã³ã¹ã¿ã³ã¹åããããªããžã§ã¯ãã§ããSelectorEventLoopã€ãã³ãã«ãŒãã®å Žåãpipe ã¯éãããã¯ã¢ãŒãã«èšå®ãããŠããªããã°ãªããŸããã
Supported pipe objects
These methods only work with objects the operating system can poll for
readiness or perform overlapped I/O on. Regular files on disk are not
supported on any platform. There is no asynchronous file I/O in asyncio;
use loop.run_in_executor() to read and write regular files without
blocking the event loop.
On Unix, with SelectorEventLoop, pipe must wrap one of the
following:
a pipe, such as an end of an
os.pipe()pair or a FIFO created withos.mkfifo();a socket;
a character device, such as a terminal.
On Windows, where only ProactorEventLoop implements these methods,
pipe must wrap a handle opened for overlapped I/O (that is, created with the
FILE_FLAG_OVERLAPPED flag), since the handle has to be associated with an
I/O completion port. Handles that were not opened for overlapped I/O are
rejected. In particular, the standard streams (sys.stdin,
sys.stdout and sys.stderr), console handles, and the pipes
created by os.pipe() are not opened for overlapped I/O and therefore
cannot be used with these methods.
泚é
SelectorEventLoop 㯠Windows äžã§äžèšã®ã¡ãœããããµããŒãããŠããŸããã Windowsã§ã¯ä»£ããã« ProactorEventLoop ã䜿ã£ãŠãã ããã
åè
loop.subprocess_exec() ããã³ loop.subprocess_shell() ã¡ãœããã
Unix ã·ã°ãã«Â¶
- loop.add_signal_handler(signum, callback, *args)¶
Set callback as the handler for the signum signal, passing args as positional arguments.
ã³ãŒã«ããã¯ã¯ loopãç»é²ãããä»ã®ã³ãŒã«ããã¯ãããã³ã€ãã³ãã«ãŒãã®å®è¡å¯èœãªã³ã«ãŒãã³ããåŒã³åºãããŸãã
signal.signal()ã䜿ã£ãŠç»é²ãããã·ã°ãã«ãã³ãã©ãšç°ãªãããã®é¢æ°ã§ç»é²ãããã³ãŒã«ããã¯ã¯ã€ãã³ãã«ãŒããšçžäºäœçšããããšãå¯èœã§ããã·ã°ãã«ãã³ããŒã誀ã£ãŠãããææäžå¯èœãªå Žå
ValueErrorãéåºãããŸãããã³ãã©ãŒã®èšå®ã«åé¡ããã£ãå ŽåRuntimeErrorãéåºãããŸããã³ãŒã«ãã㯠callback ã« ããŒã¯ãŒãåŒæ°ãæž¡ã å Žåã¯
functools.partial()ã䜿ã£ãŠãã ãããsignal.signal()ãšåããããã®é¢æ°ã¯ã¡ã€ã³ã¹ã¬ããããåŒã³åºãããªããã°ãªããŸããã
- loop.remove_signal_handler(sig)¶
ã·ã°ãã« sig ã«å¯Ÿãããã³ãã©ãåé€ããŸãã
ã·ã°ãã«ãã³ãã©ãåé€ãããå Žå
Trueãè¿ããŸããã·ã°ãã«ã«å¯ŸããŠãã³ãã©ãèšå®ãããŠããªãå Žåã«ã¯Falseãè¿ããŸããAvailability: Unix.
åè
signal ã¢ãžã¥ãŒã«ã
ã¹ã¬ãããŸãã¯ããã»ã¹ããŒã«ã§ã³ãŒããå®è¡ãã¶
- awaitable loop.run_in_executor(executor, func, *args)¶
Arrange for func to be called in the specified executor passing args as positional arguments.
The executor argument should be an
concurrent.futures.Executorinstance. The default executor is used if executor isNone. The default executor can be set byloop.set_default_executor(), otherwise, aconcurrent.futures.ThreadPoolExecutorwill be lazy-initialized and used byrun_in_executor()if needed.以äžã¯ããã°ã©ã äŸã§ã:
import asyncio import concurrent.futures def blocking_io(): # File operations (such as logging) can block the # event loop: run them in a thread pool. with open('/dev/urandom', 'rb') as f: return f.read(100) def cpu_bound(): # CPU-bound operations will block the event loop: # in general it is preferable to run them in a # process pool. return sum(i * i for i in range(10 ** 7)) async def main(): loop = asyncio.get_running_loop() ## Options: # 1. Run in the default loop's executor: result = await loop.run_in_executor( None, blocking_io) print('default thread pool', result) # 2. Run in a custom thread pool: with concurrent.futures.ThreadPoolExecutor() as pool: result = await loop.run_in_executor( pool, blocking_io) print('custom thread pool', result) # 3. Run in a custom process pool: with concurrent.futures.ProcessPoolExecutor() as pool: result = await loop.run_in_executor( pool, cpu_bound) print('custom process pool', result) # 4. Run in a custom interpreter pool: with concurrent.futures.InterpreterPoolExecutor() as pool: result = await loop.run_in_executor( pool, cpu_bound) print('custom interpreter pool', result) if __name__ == '__main__': asyncio.run(main())
Note that the entry point guard (
if __name__ == '__main__') is required for option 3 due to the peculiarities ofmultiprocessing, which is used byProcessPoolExecutor. See Safe importing of main module.ãã®ã¡ãœããã¯
asyncio.Futureãªããžã§ã¯ããè¿ããŸãã颿° func ã« ããŒã¯ãŒãåŒæ°ãæž¡ã å Žåã¯
functools.partial()ã䜿ã£ãŠãã ãããããŒãžã§ã³ 3.5.3 ã§å€æŽ:
loop.run_in_executor()ã¯å éšã§çæããã¹ã¬ããããŒã«ãšã°ãŒãã¥ãŒã¿ã®max_workersãèšå®ããã代ããã«ã¹ã¬ããããŒã«ãšã°ãŒãã¥ãŒã¿ (ThreadPoolExecutor) ã«ããã©ã«ãå€ãèšå®ãããããã«ãªããŸããã
- loop.set_default_executor(executor)¶
Set executor as the default executor used by
run_in_executor(). executor must be an instance ofThreadPoolExecutor, which includesInterpreterPoolExecutor.ããŒãžã§ã³ 3.11 ã§å€æŽ: executor must be an instance of
ThreadPoolExecutor.
Error handling API¶
ã€ãã³ãã«ãŒãå ã§ã®äŸå€ã®æ±ãæ¹ãã«ã¹ã¿ãã€ãºã§ããŸãã
- loop.set_exception_handler(handler)¶
handler ãæ°ããã€ãã³ãã«ãŒãäŸå€ãã³ãã©ãŒãšããŠèšå®ããŸãã
handler ã
Noneã®å Žåãããã©ã«ãã®äŸå€ãã³ãã©ãèšå®ãããŸããããã§ãªããã°ã handler ã¯(loop, context)ã«äžèŽãã颿°ã·ã°ããã£ãæã£ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸãããããã§loopã¯ã¢ã¯ãã£ããªã€ãã³ãã«ãŒããžã®åç §ã§ãããcontextã¯äŸå€ã®è©³çްãªèšè¿°ãããªãdictãªããžã§ã¯ãã§ã (context ã«ã€ããŠã®è©³çްã¯call_exception_handler()ã¡ãœããã®ããã¥ã¡ã³ããåç §ããŠãã ãã)ãIf the handler is called on behalf of a
TaskorHandle, it is run in thecontextvars.Contextof that task or callback handle.ããŒãžã§ã³ 3.12 ã§å€æŽ: The handler may be called in the
Contextof the task or handle where the exception originated.
- loop.get_exception_handler()¶
çŸåšã®äŸå€ãã³ãã©ãè¿ããŸããã«ã¹ã¿ã äŸå€ãã³ãã©ãèšå®ãããŠããªãå Žåã¯
Noneãè¿ããŸããAdded in version 3.5.2.
- loop.default_exception_handler(context)¶
ããã©ã«ãã®äŸå€ãã³ãã©ãŒã§ãã
ããã©ã«ãäŸå€ãã³ãã©ã¯ãäŸå€ãã³ãã©ãæªèšå®ã®å ŽåãäŸå€ãçºçããæã«åŒã³åºãããŸããããã©ã«ãäŸå€ãã³ãã©ã®æåãåãå ¥ããããã«ãã«ã¹ã¿ã äŸå€ãã³ãã©ããåŒã³åºãããšãå¯èœã§ãã
åŒæ° context ã®æå³ã¯
call_exception_handler()ãšåãã§ãã
- loop.call_exception_handler(context)¶
çŸåšã®ã€ãã³ãã«ãŒãäŸå€ãã³ãã©ãŒãåŒã³åºããŸãã
context ã¯ä»¥äžã®ããŒãå«ã
dictãªããžã§ã¯ãã§ã (å°æ¥ã® Python ããŒãžã§ã³ã§æ°ããããŒã远å ãããå¯èœæ§ããããŸã):'message': ãšã©ãŒã¡ãã»ãŒãž;
'exception' (ä»»æ): äŸå€ãªããžã§ã¯ã;
'future' (ä»»æ):
asyncio.Futureã€ã³ã¹ã¿ã³ã¹;'task' (optional):
asyncio.Taskinstance;'handle' (ä»»æ):
asyncio.Handleã€ã³ã¹ã¿ã³ã¹;'protocol' (ä»»æ): ãããã³ã« ã€ã³ã¹ã¿ã³ã¹;
'transport' (ä»»æ): ãã©ã³ã¹ããŒã ã€ã³ã¹ã¿ã³ã¹;
'socket' (optional):
socket.socketinstance;'source_traceback' (optional): Traceback of the source;
'handle_traceback' (optional): Traceback of the handle;
- 'asyncgen' (optional): Asynchronous generator that caused
the exception.
泚é
This method should not be overloaded in subclassed event loops. For custom exception handling, use the
set_exception_handler()method.
ãããã°ã¢ãŒãã®æå¹å¶
- loop.get_debug()¶
ã€ãã³ãã«ãŒãã®ãããã°ã¢ãŒã (
bool) ãååŸããŸããç°å¢å€æ°
PYTHONASYNCIODEBUGã«ç©ºã§ãªãæååãèšå®ãããŠããå Žåã®ããã©ã«ãå€ã¯Trueãããã§ãªãå Žåã¯Falseã«ãªããŸãã
- loop.set_debug(enabled: bool)¶
ã€ãã³ãã«ãŒãã®ãããã°ã¢ãŒããèšå®ããŸãã
ããŒãžã§ã³ 3.7 ã§å€æŽ: æ°ãã Python éçºã¢ãŒã ã䜿ã£ãŠãããã°ã¢ãŒããæå¹åããããšãã§ããããã«ãªããŸããã
- loop.slow_callback_duration¶
This attribute can be used to set the minimum execution duration in seconds that is considered "slow". When debug mode is enabled, "slow" callbacks are logged.
Default value is 100 milliseconds.
åè
Running subprocesses¶
ãã®ç¯ã§è§£èª¬ããŠããã®ã¯äœæ°Žæºã®ã¡ãœããã§ããéåžžã® async/await ã³ãŒãã§ã¯ã髿°Žæºã®é¢æ°ã§ãã asyncio.create_subprocess_shell() ã asyncio.create_subprocess_exec() ã代ããã«äœ¿ãããšãæ€èšããŠãã ããã
泚é
On Windows, the default event loop ProactorEventLoop supports
subprocesses, whereas SelectorEventLoop does not. See
Subprocess Support on Windows for
details.
- async loop.subprocess_exec(protocol_factory, *args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)¶
args ã§æå®ãããã²ãšã€ã®ããŸãã¯è€æ°ã®æåååŒæ°ãããµãããã»ã¹ãçæããŸãã
args ã¯äžèšã®ããããã«åœãŠã¯ãŸãæååã®ãªã¹ãã§ãªããã°ãªããŸãã:
str;ãŸã㯠ãã¡ã€ã«ã·ã¹ãã ã®ãšã³ã³ãŒãã£ã³ã° ã«ãšã³ã³ãŒãããã
bytes
åŒæ°ã®æåã®æååã¯ããã°ã©ã ã®å®è¡ãã¡ã€ã«ãæå®ããŸããããã«ç¶ãæ®ãã®æååã¯åŒæ°ãæå®ãããã®ããã°ã©ã ã«å¯Ÿãã
argvãæ§æããŸãããã®ã¡ãœããã¯æšæºã©ã€ãã©ãªã®
subprocess.Popenã¯ã©ã¹ããshell=Falseãã€æåã®åŒæ°ã«æååã®ãªã¹ããæž¡ããŠåŒã³åºããå Žåã«äŒŒãŠããŸããããããPopenã¯ã©ã¹ã¯æååã®ãªã¹ããåŒæ°ãšããŠã²ãšã€ã ãåãã®ã«å¯ŸããŠã subprocess_exec ã¯è€æ°ã®æåååŒæ°ããšãããšãã§ããŸããprotocol_factory ã¯
asyncio.SubprocessProtocolã¯ã©ã¹ã®æŽŸçã¯ã©ã¹ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããããã®ä»ã®åŒæ°:
stdin ã¯äžèšã®ããããããšãããšãã§ããŸã:
a file-like object
an existing file descriptor (a positive integer), for example those created with
os.pipe()ããã©ã«ãå€ã¯
subprocess.PIPE宿°ã§ããã®å Žåæ°èŠã«ãã€ããçæããŠæ¥ç¶ããŸããNoneãèšå®ãããå Žåããµãããã»ã¹ã¯å ã®ããã»ã¹ã®ãã¡ã€ã«ãã¹ã¯ãªãã¿ãåŒãç¶ããŸããsubprocess.DEVNULL宿°ãèšå®ãããšãç¹å¥ãªãã¡ã€ã«os.devnullã䜿ããŸãã
stdout ã¯äžèšã®ããããããšãããšãã§ããŸã:
a file-like object
ããã©ã«ãå€ã¯
subprocess.PIPE宿°ã§ããã®å Žåæ°èŠã«ãã€ããçæããŠæ¥ç¶ããŸããNoneãèšå®ãããå Žåããµãããã»ã¹ã¯å ã®ããã»ã¹ã®ãã¡ã€ã«ãã¹ã¯ãªãã¿ãåŒãç¶ããŸããsubprocess.DEVNULL宿°ãèšå®ãããšãç¹å¥ãªãã¡ã€ã«os.devnullã䜿ããŸãã
stderr ã¯äžèšã®ããããããšãããšãã§ããŸã:
a file-like object
ããã©ã«ãå€ã¯
subprocess.PIPE宿°ã§ããã®å Žåæ°èŠã«ãã€ããçæããŠæ¥ç¶ããŸããNoneãèšå®ãããå Žåããµãããã»ã¹ã¯å ã®ããã»ã¹ã®ãã¡ã€ã«ãã¹ã¯ãªãã¿ãåŒãç¶ããŸããsubprocess.DEVNULL宿°ãèšå®ãããšãç¹å¥ãªãã¡ã€ã«os.devnullã䜿ããŸããsubprocess.STDOUT宿°ãèšå®ãããšãæšæºãšã©ãŒåºåã¹ããªãŒã ãããã»ã¹ã®æšæºåºåã¹ããªãŒã ã«æ¥ç¶ããŸãã
ãã®ä»ã®ãã¹ãŠã®ããŒã¯ãŒãåŒæ°ã¯è§£éãããã«ãã®ãŸãŸ
subprocess.Popenã«æž¡ãããŸãããã ãã bufsizeã universal_newlinesã shellã textã encoding ããã³ errors ã¯æå®ããŠã¯ãããŸãããasyncioã®ãµãããã»ã¹ API ã¯ã¹ããªãŒã ããããã¹ããžã®ãã³ãŒãããµããŒãããŠããŸãããã¹ããªãŒã ããããã¹ãã«å€æããã«ã¯bytes.decode()颿°ã䜿ã£ãŠãã ããã
If a file-like object passed as stdin, stdout or stderr represents a pipe, then the other side of this pipe should be registered with
connect_write_pipe()orconnect_read_pipe()for use with the event loop.ä»ã®åŒæ°ã«ã€ããŠã®è©³çްã¯
subprocess.Popenã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãåç §ããŠãã ããã(transport, protocol)ã®ãã¢ãè¿ããŸããããã§ transport ã¯asyncio.SubprocessTransportåºåºã¯ã©ã¹ã«é©åãããªããžã§ã¯ãã§ã protocol 㯠protocol_factory ã«ããã€ã³ã¹ã¿ã³ã¹åããããªããžã§ã¯ãã§ããIf the transport is closed or is garbage collected, the child process is killed if it is still running.
- async loop.subprocess_shell(protocol_factory, cmd, *, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)¶
ã³ãã³ã cmd ãããã©ãããã©ãŒã ã® "ã·ã§ã«" ã·ã³ã¿ãã¯ã¹ã䜿ã£ãŠãµãããã»ã¹ãçæããŸãã cmd ã¯
stræååããã㯠ãã¡ã€ã«ã·ã¹ãã ã®ãšã³ã³ãŒãã£ã³ã° ã§ãšã³ã³ãŒããããbytesæååã§ããããã¯æšæºã©ã€ãã©ãªã®
subprocess.Popenã¯ã©ã¹ãshell=Trueã§åŒã³åºããå ŽåãšäŒŒãŠããŸããprotocol_factory ã¯
SubprocessProtocolã®æŽŸçã¯ã©ã¹ãè¿ãåŒã³åºãå¯èœãªããžã§ã¯ãã§ãªããã°ãªããŸããããã®ä»ã®åŒæ°ã«ã€ããŠã®è©³çްã¯
subprocess_exec()ã¡ãœãããåç §ããŠãã ããã(transport, protocol)ã®ãã¢ãè¿ããŸããããã§ transport ã¯SubprocessTransportåºåºã¯ã©ã¹ã«é©åãããªããžã§ã¯ãã§ã protocol 㯠protocol_factory ã«ããã€ã³ã¹ã¿ã³ã¹åããããªããžã§ã¯ãã§ããIf the transport is closed or is garbage collected, the child process is killed if it is still running.
泚é
ã·ã§ã«ã€ã³ãžã§ã¯ã·ã§ã³ ã®è匱æ§ãåé¿ããããã«å
šãŠã®ç©ºçœæåããã³ç¹æ®æåãé©åã«ã¯ãªãŒãããããšã¯ãã¢ããªã±ãŒã·ã§ã³åŽã®è²¬ä»»ã§ç¢ºå®ã«è¡ã£ãŠãã ãããã·ã§ã«ã³ãã³ããæ§æããæååå
ã®ç©ºçœæåãšç¹æ®æåã®ãšã¹ã±ãŒãã¯ã shlex.quote() 颿°ã䜿ããšé©åã«è¡ãããšãã§ããŸãã
Callback handles¶
- class asyncio.Handle¶
loop.call_soon()ãloop.call_soon_threadsafe()ãè¿ãã³ãŒã«ããã¯ã®ã©ãããŒã§ãã- get_context()¶
Return the
contextvars.Contextobject associated with the handle.Added in version 3.12.
- cancel()¶
ã³ãŒã«ããã¯ããã£ã³ã»ã«ããŸããã³ãŒã«ããã¯ããã£ã³ã»ã«æžã¿ãŸãã¯å®è¡æžã¿ã®å Žåããã®ã¡ãœããã¯äœã®åœ±é¿ããããŸããã
- cancelled()¶
ã³ãŒã«ããã¯ããã£ã³ã»ã«ãããå Žå
Trueãè¿ããŸããAdded in version 3.7.
- class asyncio.TimerHandle¶
A callback wrapper object returned by
loop.call_later(), andloop.call_at().ãã®ã¯ã©ã¹ã¯
Handleã®æŽŸçã¯ã©ã¹ã§ãã- when()¶
ã³ãŒã«ããã¯ã®ã¹ã±ãžã¥ãŒã«æå»ãç§åäœã®
floatã§è¿ããŸããæ»ãå€ã®æå»ã¯çµ¶å¯Ÿå€ã§ã
loop.time()ãšåãåç §æå»ã䜿ã£ãŠå®çŸ©ãããŠããŸããAdded in version 3.7.
Server objects¶
Server ãªããžã§ã¯ã㯠loop.create_server()ã loop.create_unix_server()ã start_server() ããã³ start_unix_server() 颿°ã«ããçæãããŸãã
Do not instantiate the Server class directly.
- class asyncio.Server¶
Server ãªããžã§ã¯ãã¯éåæã®ã³ã³ããã¹ããããŒãžã£ã§ãã
async withæã®äžã§äœ¿ãããå Žåãasync withæãå®äºããæã« Server ãªããžã§ã¯ããã¯ããŒãºãããããšãããã³ãã以éã«æ¥ç¶ãåãä»ããªãããšãä¿èšŒãããŸããsrv = await loop.create_server(...) async with srv: # some code # At this point, srv is closed and no longer accepts new connections.
ããŒãžã§ã³ 3.7 ã§å€æŽ: Python 3.7 ããã Server ãªããžã§ã¯ãã¯éåæã®ã³ã³ããã¹ããããŒãžã£ã«ãªããŸããã
ããŒãžã§ã³ 3.11 ã§å€æŽ: This class was exposed publicly as
asyncio.Serverin Python 3.9.11, 3.10.3 and 3.11.- close()¶
ãµãŒããŒã忢ããŸã: åŸ æ©ããŠãããœã±ãããã¯ããŒãºã
sockets屿§ã«Noneãèšå®ããŸããæ¢åã®åä¿¡äžã®ã¯ã©ã€ã¢ã³ããšã®æ¥ç¶ã衚ããœã±ããã¯ãªãŒãã³ã®ãŸãŸã§ãã
The server is closed asynchronously; use the
wait_closed()coroutine to wait until the server is closed (and no more connections are active).
- close_clients()¶
Close all existing incoming client connections.
Calls
close()on all associated transports.close()should be called beforeclose_clients()when closing the server to avoid races with new clients connecting.Added in version 3.13.
- abort_clients()¶
Close all existing incoming client connections immediately, without waiting for pending operations to complete.
Calls
abort()on all associated transports.close()should be called beforeabort_clients()when closing the server to avoid races with new clients connecting.Added in version 3.13.
- get_loop()¶
ãµãŒããªããžã§ã¯ãã«ä»éããã€ãã³ãã«ãŒããè¿ããŸãã
Added in version 3.7.
- async start_serving()¶
æ¥ç¶ã®åãä»ããéå§ããŸãã
This method is idempotent, so it can be called when the server is already serving.
ããŒã¯ãŒãå°çšã®ãã©ã¡ãŒã¿ start_serving ã
loop.create_server()ãasyncio.start_server()ã¡ãœããã«å¯ŸããŠäœ¿çšããããšã«ãããåæã«æ¥ç¶ãåãä»ããªã Server ãªããžã§ã¯ããçæããããšãã§ããŸãããã®å ŽåServer.start_serving()ãŸãã¯Server.serve_forever()ã¡ãœããã䜿ã£ãŠãªããžã§ã¯ããæ¥ç¶ã®åãä»ããéå§ããããã«ããããšãã§ããŸããAdded in version 3.7.
- async serve_forever()¶
æ¥ç¶ã®åãå ¥ããéå§ããã³ã«ãŒãã³ããã£ã³ã»ã«ããããŸã§ç¶ç¶ããŸãã
serve_foreverã¿ã¹ã¯ã®ãã£ã³ã»ã«ã«ãããµãŒããŒãã¯ããŒãºãããŸãããã®ã¡ãœããã¯ãµãŒããŒããã§ã«æ¥ç¶ã®åãå ¥ããéå§ããŠããŠãåŒã³åºãå¯èœã§ããã²ãšã€ã® Server ãªããžã§ã¯ãã«ã€ã
serve_foreverã¿ã¹ã¯ã¯ã²ãšã€ã ãååšã§ããŸãã以äžã¯ããã°ã©ã äŸã§ã:
async def client_connected(reader, writer): # Communicate with the client with # reader/writer streams. For example: await reader.readline() async def main(host, port): srv = await asyncio.start_server( client_connected, host, port) await srv.serve_forever() asyncio.run(main('127.0.0.1', 0))
Added in version 3.7.
- is_serving()¶
ãµãŒããŒãæ°èŠã«æ¥ç¶ã®åãå ¥ããéå§ããå Žå
Trueãè¿ããŸããAdded in version 3.7.
- async wait_closed()¶
Wait until the
close()method completes and all active connections have finished.ããŒãžã§ã³ 3.12 ã§å€æŽ:
wait_closed()now waits until the server is closed and all active connections have finished. Previously, it returned immediately if the server was already closed, even if connections were still active.
- sockets¶
List of socket-like objects,
asyncio.trsock.TransportSocket, which the server is listening on.ããŒãžã§ã³ 3.7 ã§å€æŽ: Python 3.7 ããåã®ããŒãžã§ã³ã§ã¯ã
Server.socketsã¯å éšã«æã£ãŠãããµãŒããŒãœã±ããã®ãªã¹ããçŽæ¥è¿ããŠããŸããã Python 3.7 ã§ã¯ãªã¹ãã®ã³ããŒãè¿ãããããã«ãªããŸããã
Event loop implementations¶
asyncio ã¯2ã€ã®ç°ãªãã€ãã³ãã«ãŒãã®å®è£
ã SelectorEventLoop ãš ProactorEventLoopã ãæäŸããŸã:
By default asyncio is configured to use EventLoop.
- class asyncio.SelectorEventLoop¶
A subclass of
AbstractEventLoopbased on theselectorsmodule.ãã©ãããã©ãŒã äžã§å©çšå¯èœãªæãå¹çã®è¯ã selector ã䜿ããŸããç¹å®ã®ã»ã¬ã¯ã¿å®è£ ã䜿ãããã«æåã§æ§æããããšãå¯èœã§ã:
import asyncio import selectors async def main(): ... loop_factory = lambda: asyncio.SelectorEventLoop(selectors.SelectSelector()) asyncio.run(main(), loop_factory=loop_factory)
Availability: Unix, Windows.
- class asyncio.ProactorEventLoop¶
A subclass of
AbstractEventLoopfor Windows that uses "I/O Completion Ports" (IOCP).Availability: Windows.
- class asyncio.EventLoop¶
An alias to the most efficient available subclass of
AbstractEventLoopfor the given platform.It is an alias to
SelectorEventLoopon Unix andProactorEventLoopon Windows.Added in version 3.13.
- class asyncio.AbstractEventLoop¶
asyncio ã«é©åããã€ãã³ãã«ãŒãã®æœè±¡åºåºã¯ã©ã¹ã§ãã
The Event loop methods section lists all methods that an alternative implementation of
AbstractEventLoopshould have defined.
䜿çšäŸÂ¶
ãã®ç¯ã®å
šãŠã®äœ¿çšäŸã¯ æå³çã« loop.run_forever() ã loop.call_soon() ã®ãããª äœæ°Žæºã®ã€ãã³ãã«ãŒã API ã®äœ¿çšæ³ã瀺ããŠããŸããäžæ¹ã§çŸä»£ç㪠asyncio ã¢ããªã±ãŒã·ã§ã³ã¯ããã«ç€ºããããªæ¹æ³ãã»ãšãã©å¿
èŠãšããŸããã asyncio.run() ã®ãããªé«æ°Žæºã®é¢æ°ã®äœ¿çšãæ€èšããŠãã ããã
call_soon() ã䜿ã£ã Hello World¶
loop.call_soon() ã¡ãœããã䜿ã£ãŠã³ãŒã«ããã¯ãã¹ã±ãžã¥ãŒã«ããäŸã§ããã³ãŒã«ããã¯ã¯ "Hello World" ãåºåãã€ãã³ãã«ãŒãã忢ããŸã:
import asyncio
def hello_world(loop):
"""A callback to print 'Hello World' and stop the event loop"""
print('Hello World')
loop.stop()
loop = asyncio.new_event_loop()
# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)
# Blocking call interrupted by loop.stop()
try:
loop.run_forever()
finally:
loop.close()
åè
ã³ã«ãŒãã³ãš run() 颿°ã䜿çšããåããã㪠Hello World ã®äŸã
call_later() ã§çŸåšã®æ¥æã衚瀺ãã¶
æ¯ç§çŸåšæå»ã衚瀺ããã³ãŒã«ããã¯ã®äŸã§ããã³ãŒã«ããã¯ã¯ loop.call_later() ã¡ãœããã䜿ã£ãŠèªèº«ã5ç§åŸã«å®è¡ããããåã¹ã±ãžã¥ãŒã«ããã€ãã³ãã«ãŒãã忢ããŸã:
import asyncio
import datetime as dt
def display_date(end_time, loop):
print(dt.datetime.now())
if (loop.time() + 1.0) < end_time:
loop.call_later(1, display_date, end_time, loop)
else:
loop.stop()
loop = asyncio.new_event_loop()
# Schedule the first call to display_date()
end_time = loop.time() + 5.0
loop.call_soon(display_date, end_time, loop)
# Blocking call interrupted by loop.stop()
try:
loop.run_forever()
finally:
loop.close()
åè
ã³ã«ãŒãã³ãš run() 颿°ã䜿çšããåããã㪠çŸåšæå»åºå ã®äŸã
èªã¿èŸŒã¿ã€ãã³ãçšãã¡ã€ã«èšè¿°åã®ç£èŠÂ¶
ãã¡ã€ã«èšè¿°åã loop.add_reader() ã¡ãœããã䜿ã£ãŠäœããã®ããŒã¿ãåä¿¡ãããŸã§åŸ
æ©ãããã®åŸã€ãã³ãã«ãŒããã¯ããŒãºããŸã:
import asyncio
from socket import socketpair
# Create a pair of connected file descriptors
rsock, wsock = socketpair()
loop = asyncio.new_event_loop()
def reader():
data = rsock.recv(100)
print("Received:", data.decode())
# We are done: unregister the file descriptor
loop.remove_reader(rsock)
# Stop the event loop
loop.stop()
# Register the file descriptor for read event
loop.add_reader(rsock, reader)
# Simulate the reception of data from the network
loop.call_soon(wsock.send, 'abc'.encode())
try:
# Run the event loop
loop.run_forever()
finally:
# We are done. Close sockets and the event loop.
rsock.close()
wsock.close()
loop.close()
åè
ãã©ã³ã¹ããŒãããããã³ã«ãããã³
loop.create_connection()ã¡ãœããã䜿çšããåããã㪠äŸã髿°Žæºã®
asyncio.open_connection()颿°ãšã¹ããªãŒã ã䜿çšããããã²ãšã€ã® å®è£ äŸã
SIGINT ããã³ SIGTERM çšã®ã·ã°ãã«ãã³ãã©ãŒã®èšå®Â¶
(This signal example only works on Unix.)
Register handlers for signals SIGINT and SIGTERM
using the loop.add_signal_handler() method:
import asyncio
import functools
import os
import signal
def ask_exit(signame, loop):
print("got signal %s: exit" % signame)
loop.stop()
async def main():
loop = asyncio.get_running_loop()
for signame in {'SIGINT', 'SIGTERM'}:
loop.add_signal_handler(
getattr(signal, signame),
functools.partial(ask_exit, signame, loop))
await asyncio.sleep(3600)
print("Event loop running for 1 hour, press Ctrl+C to interrupt.")
print(f"pid {os.getpid()}: send SIGINT or SIGTERM to exit.")
asyncio.run(main())