-
Notifications
You must be signed in to change notification settings - Fork 240
/
echoserv.py
124 lines (106 loc) · 3.24 KB
/
echoserv.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
from curio import *
from curio.socket import *
import signal
from curio import subprocess
async def monitor():
async with SignalSet(signal.SIGUSR1, signal.SIGUSR2) as sigset:
while True:
try:
signo = await sigset.wait(timeout=30)
print('Caught signal', signo)
print('Ignoring signals for 30 seconds')
with sigset.ignore():
await sleep(30)
except TimeoutError:
print('No signal')
except CancelledError:
print('Cancelled')
return
async def subproc():
p = subprocess.Popen(['python3', 'slow.py'], stdout=subprocess.PIPE)
try:
await p.wait(timeout=10)
except subprocess.TimeoutExpired:
print('Not done yet')
try:
await p.wait(timeout=10)
except subprocess.TimeoutExpired:
print('Still not done yet')
p.terminate()
while True:
line = await p.stdout.readline()
if not line:
break
print('subproc', line)
await p.wait()
print('Subproc done')
text = '''
This is some test code
More test
code
Some test input
'''
async def subproc1():
p = subprocess.Popen(['wc'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
stdout, stderr = await p.communicate(text.encode('ascii'))
print(':::stdout:::')
print(stdout)
print(':::stderr::')
print(stderr)
async def subproc2():
p = Popen(['python3', 'slow.py'], stdout=subprocess.PIPE)
stdout, _ = await p.communicate(timeout=10)
await p.wait()
print('Subproc done')
print(stdout)
async def subproc3():
try:
out = await run_subprocess(['python3', 'slow.py'], timeout=10)
print(out.stdout)
print('return code', out.returncode)
print(out)
except subprocess.CalledProcessError as e:
print('Failed', e.returncode)
print(e.stderr)
print('Subproc done')
async def spinner(prefix, interval):
n = 0
while True:
await sleep(interval)
print(prefix, n)
n += 1
async def main(address):
task = await new_task(echo_server(address))
# await SignalSet(signal.SIGINT).wait()
# print('You hit control-C')
# await task.cancel()
# print('Shutdown complete')
await task.join()
async def echo_server(address):
sock = socket(AF_INET, SOCK_STREAM)
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
with sock:
while True:
client, addr = await sock.accept()
print('Connection from', addr)
await new_task(echo_client(client))
del client
async def echo_client(client):
with client.makefile('rwb') as client_f:
try:
async for line in client_f:
await client_f.write(line)
except CancelledError:
await client_f.write(b'Server going down\n')
print('Connection closed')
if __name__ == '__main__':
import os
print('pid', os.getpid())
kernel = get_kernel()
kernel.add_task(main(('',25000)))
kernel.add_task(monitor())
kernel.add_task(subproc())
kernel.add_task(spinner('spin',1))
kernel.run(pdb=True)