-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
134 lines (99 loc) · 3.14 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import asyncio
import logging
import traceback
import objc
from objc_asyncio import PyObjCEventLoop
objc.options.verbose = True
logging.basicConfig(level=logging.DEBUG)
async def printer():
print("******* start")
await asyncio.sleep(1)
print("******* middle")
await asyncio.sleep(1)
print("******* end")
async def main():
print("******* hello")
await printer()
print("******* world")
el.stop()
async def run(cmd):
proc = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await proc.communicate()
print(f"[{cmd!r} exited with {proc.returncode}]")
if stdout:
print(f"[stdout]\n{stdout.decode()}")
if stderr:
print(f"[stderr]\n{stderr.decode()}")
el.stop()
def handler():
print("Signal")
el.stop()
async def resolver():
print("Start resolving")
info = await el.getaddrinfo("www.rivm.nl", 80)
print(info)
el.stop()
async def clock():
while True:
print("*")
await asyncio.sleep(1)
def exec_func():
print("*** Executing task")
# return sys.version_info
return 42
async def executor():
print("*** start executing in executor")
try:
value = await el.run_in_executor(None, exec_func)
print("*** done:", value)
except: # noqa: B001, E722
print("*** error")
traceback.print_exc()
async def basic_socket():
try:
host = "www.python.org"
port = 80
print(f"Opening connection to {host}:{port}")
reader, writer = await asyncio.open_connection(host, port)
# reader = asyncio.StreamReader(limit=1000, loop=el)
# print("reader", reader)
# protocol = asyncio.StreamReaderProtocol(reader, loop=el)
# print("protocol", protocol)
# transport, _ = await el.create_connection(lambda: protocol, host, port)
# print("transport", transport)
# writer = asyncio.StreamWriter(transport, protocol, reader, el)
# print("writer", writer)
print(f"Sending request")
writer.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % (host.encode("utf-8"),))
await writer.drain()
data = await reader.read(800000)
print(f"Received: {data.decode()!r}")
print("Close the connection")
writer.close()
await writer.wait_closed()
except: # noqa: E722, B001
traceback.print_exc()
raise
finally:
el.stop()
el = PyObjCEventLoop()
# el = asyncio.get_event_loop()
# el.set_debug(True)
asyncio.set_event_loop(el)
# el.add_signal_handler(signal.SIGTERM, handler)
# el.add_signal_handler(signal.SIGUSR1, handler)
# el.call_soon(lambda a: print(a), "hello world")
# el.call_later(1.0, lambda: print("timer 1"))
# el.call_later(2.0, lambda: print("timer 2"))
# task = el.create_task(main())
# el.call_later(5, lambda: el.stop())
# el.run_until_complete(task)
# task = el.create_task(run("ls -1"))
# task = el.create_task(clock())
# task = el.create_task(resolver())
# task = el.create_task(executor())
# task = el.create_task(main())
task = el.create_task(basic_socket())
el.run_forever()