-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8_tasks.py
executable file
·61 lines (45 loc) · 1.3 KB
/
8_tasks.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
#!/usr/bin/env python3.5
import select
import socket
from itertools import count
tasks = {}
poll = select.poll()
def makesocket(*address):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(16)
sock.setblocking(False)
return sock
def server(sock):
print("Waiting for connections", sock)
while True:
yield sock.fileno(), select.EPOLLIN
client, address = sock.accept()
create_task(echo(client))
def echo(client):
print("Client connected", client)
for i in count():
yield client.fileno(), select.EPOLLOUT
client.sendall(b"%i> " % i)
yield client.fileno(), select.EPOLLIN
buffer = client.recv(1024)
if not buffer:
break
client.sendall(buffer)
print("Client disconnected", client)
def create_task(task):
fileno, eventmask = next(task)
tasks[fileno] = task
poll.register(fileno, eventmask)
# "start" the server
create_task(server(makesocket('localhost', 1234)))
# main loop
while True:
for fileno, event in poll.poll():
poll.unregister(fileno)
try:
task = tasks.pop(fileno)
create_task(task)
except StopIteration:
pass