This repository was archived by the owner on Dec 2, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +7
-16
lines changed
18-asyncio-py3.7/charfinder Expand file tree Collapse file tree 1 file changed +7
-16
lines changed Original file line number Diff line number Diff line change @@ -38,26 +38,17 @@ async def handle_queries(reader, writer): # <3>
38
38
# END TCP_CHARFINDER_TOP
39
39
40
40
# BEGIN TCP_CHARFINDER_MAIN
41
- def main (address = '127.0.0.1' , port = 2323 ): # <1>
41
+ async def main (address = '127.0.0.1' , port = 2323 ): # <1>
42
42
port = int (port )
43
- loop = asyncio .get_event_loop ()
44
- server_coro = asyncio .start_server (handle_queries , address , port ,
45
- loop = loop ) # <2>
46
- server = loop .run_until_complete (server_coro ) # <3>
43
+ server = await asyncio .start_server (handle_queries , address , port ) # <2>
47
44
48
- host = server .sockets [0 ].getsockname () # <4>
49
- print ('Serving on {}. Hit CTRL-C to stop.' .format (host )) # <5>
50
- try :
51
- loop .run_forever () # <6>
52
- except KeyboardInterrupt : # CTRL+C pressed
53
- pass
45
+ host = server .sockets [0 ].getsockname () # <3>
46
+ print ('Serving on {}. Hit CTRL-C to stop.' .format (host )) # <4>
54
47
55
- print ('Server shutting down.' )
56
- server .close () # <7>
57
- loop .run_until_complete (server .wait_closed ()) # <8>
58
- loop .close () # <9>
48
+ async with server :
49
+ await server .serve_forever ()
59
50
60
51
61
52
if __name__ == '__main__' :
62
- main (* sys .argv [1 :]) # <10 >
53
+ asyncio . run ( main (* sys .argv [1 :])) # <5 >
63
54
# END TCP_CHARFINDER_MAIN
You can’t perform that action at this time.
0 commit comments