public
Last active

  • Download Gist
gevent_sendall.py
Python
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
 
import sys
import gevent
from gevent.coros import RLock
from gevent.server import StreamServer
from gevent.socket import create_connection
 
lock = RLock()
 
 
def _send(socket, data):
with lock:
socket.sendall(data)
 
 
def handle_tcp(socket, address):
print 'new connection'
length = 100
data1 = '1' * 1024 * 1024 * length
data2 = '2' * 1024 * 1024 * length
g1 = gevent.spawn(_send, socket, data1)
g2 = gevent.spawn(_send, socket, data2)
gevent.joinall([g1, g2])
print 'data sent'
 
 
def client_connect(address):
socket = create_connection(address)
while True:
data = socket.recv(1024 * 1024)
if not data:
print
print 'Data received'
break
else:
sys.stdout.write(data[0])
 
 
tcp_server = StreamServer(('127.0.0.1', 1234), handle_tcp)
gevent.spawn(client_connect, ('127.0.0.1', 1234))
tcp_server.serve_forever()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.