Created
January 26, 2018 03:08
-
-
Save Kcrong/ecb919bfbd45fe186899554b628e6db4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
1. main 함수에서 `async def A`를 호출 후 await 하고, 반환값을 받아 다시 `async def B`를 호출하는 방식 | |
""" | |
import asyncio | |
from socket import socket, AF_INET, SOCK_STREAM | |
async def receive_print(client_sock, client_addr): | |
return "%s:%d said %s" % (client_addr[0], client_addr[1], client_sock.recv(1024).decode().strip()) | |
async def accept(serv_sock_): | |
return serv_sock_.accept() | |
async def main(): | |
serv_sock = socket(AF_INET, SOCK_STREAM) | |
serv_sock.bind(('localhost', 8081)) | |
serv_sock.listen(10) | |
print(await receive_print(*await accept(serv_sock))) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
# ------------------------------------------------------------------------------------------------------------ | |
""" | |
2. `async def A`가 `async def B` 를 안에서 호출하고, 한번에 반환하는 방식 | |
""" | |
async def receive_print(client_sock, client_addr): | |
return "%s:%d said %s" % (client_addr[0], client_addr[1], client_sock.recv(1024).decode().strip()) | |
async def accept(serv_sock_): | |
return await receive_print(*serv_sock_.accept()) | |
async def main(): | |
serv_sock = socket(AF_INET, SOCK_STREAM) | |
serv_sock.bind(('localhost', 8081)) | |
serv_sock.listen(10) | |
print(await accept(serv_sock)) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment