-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimple_async_cli.py
More file actions
40 lines (31 loc) · 862 Bytes
/
Copy pathsimple_async_cli.py
File metadata and controls
40 lines (31 loc) · 862 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 3 13:32:02 2016
@author: thor
"""
import socket
from time import time
from tornado import ioloop
loop = ioloop.IOLoop.current()
socks = [socket.socket( socket.AF_INET, socket.SOCK_STREAM ) for _ in range( 50 )]
[sock.connect(( 'localhost', 8760 )) for sock in socks]
SockD = { sock.fileno(): sock for sock in socks }
t0 = time()
n = 0
def OnEvent( fd, event ):
if event == loop.READ:
sock = SockD[fd]
sock.recv( 99 )
global n
n += 1
if n >= 1000:
print 'time cost', time() - t0
sock.close()
loop.remove_handler( fd )
loop.stop()
return
sock.send( 'test message.' )
for fd, sock in SockD.items():
loop.add_handler( fd, OnEvent, loop.READ )
sock.send( 'test message.' )
loop.start()