def handler(local):
    client = str(local.getHost())
    print 'accepted connection from %s' % client
    remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP(remote_host, remote_port)
    a = proc.spawn(forward, remote, local)
    b = proc.spawn(forward, local, remote)
    proc.waitall([a, b], trap_errors=True)
    print 'closed connection to %s' % client
def handler(local):
    client = str(local.getHost())
    print 'accepted connection from %s' % client
    remote = GreenClientCreator(reactor, UnbufferedTransport).connectTCP(
        remote_host, remote_port)
    a = proc.spawn(forward, remote, local)
    b = proc.spawn(forward, local, remote)
    proc.waitall([a, b], trap_errors=True)
    print 'closed connection to %s' % client
示例#3
0
 def test_wait_all_exception_order(self):
     # if there're several exceptions raised, the earliest one must be raised by wait
     def first():
         sleep(0.1)
         raise ExpectedError('first')
     a = proc.spawn(first)
     b = proc.spawn(lambda : getcurrent().throw(ExpectedError('second')))
     try:
         proc.waitall([a, b])
     except ExpectedError, ex:
         assert 'second' in str(ex), repr(str(ex))
示例#4
0
 def test_wait_all_exception_order(self):
     # if there're several exceptions raised, the earliest one must be raised by wait
     def badint():
         sleep(0.1)
         int('first')
     a = proc.spawn(badint)
     b = proc.spawn(int, 'second')
     try:
         proc.waitall([a, b])
     except ValueError, ex:
         assert 'second' in str(ex), repr(str(ex))
示例#5
0
 def test_wait_noerrors(self):
     x = proc.spawn(lambda : 1)
     y = proc.spawn(lambda : 2)
     z = proc.spawn(lambda : 3)
     self.assertEqual(proc.waitall([x, y, z]), [1, 2, 3])
     e = coros.event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     x.unlink(e)
     e = coros.event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     self.assertEqual([proc.waitall([X]) for X in [x, y, z]], [[1], [2], [3]])
示例#6
0
 def test_wait_noerrors(self):
     x = proc.spawn(lambda : 1)
     y = proc.spawn(lambda : 2)
     z = proc.spawn(lambda : 3)
     self.assertEqual(proc.waitall([x, y, z]), [1, 2, 3])
     e = _event.Event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     x.unlink(e)
     e = _event.Event()
     x.link(e)
     self.assertEqual(e.wait(), 1)
     self.assertEqual([proc.waitall([X]) for X in [x, y, z]], [[1], [2], [3]])
示例#7
0
 def test_write_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     self._test_write_chunk(client, server)
     self._test_write_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.loseConnection()
     server.loseConnection()
示例#8
0
 def test_close_connection__read(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client.loseConnection()
     self.assertRaises(ConnectionDone, server.read_chunk)
     self.assertRaises(ConnectionDone, server.write, self.make_hello(server).encode())
     self.assertRaises(ConnectionDone, client.read_chunk)
     self.assertRaises(ConnectionDone, client.write, self.make_hello(client).encode())
示例#9
0
 def test_deliver_chunk(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     self._test_deliver_chunk(client, server)
     self._test_deliver_chunk(server, client)
     #self.assertNoIncoming(0.1, client, server)
     client.shutdown()
     server.shutdown()
示例#10
0
 def test_close_connection__receive(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     assert isinstance(client, MSRPTransport), repr(client)
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.shutdown()
     self.assertRaises(ConnectionDone, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
     self.assertRaises(ConnectionDone, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
示例#11
0
 def test_send_chunk_response_localtimeout(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession_ZeroTimeout(client), GreenMSRPSession(server)
     x = self.make_hello(client.msrp)
     self.assertRaisesCode(LocalResponse, 408, client.deliver_chunk, x)
     y = server.receive_chunk()
     self.assertSameData(x, y)
     #self.assertNoIncoming(0.1, client, server)
     server.shutdown()
     client.shutdown()
示例#12
0
 def test_reader_failed__receive(self):
     # if reader fails with an exception, receive_chunk raises that exception
     # send_chunk raises an error and the other party gets closed connection
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     self.assertRaises(InjectedError, client.receive_chunk)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
示例#13
0
 def test_reader_failed__send(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     client.reader_job.kill(InjectedError("Killing client's reader_job"))
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, client.send_chunk, self.make_hello(client.msrp))
     self.assertRaises(InjectedError, client.receive_chunk)
     api.sleep(0.1)
     self.assertRaises(MSRPSessionError, server.send_chunk, self.make_hello(server.msrp))
     self.assertRaises(ConnectionClosed, server.receive_chunk)
示例#14
0
 def test_deliver_chunk_success_report(self):
     client, server = proc.waitall(self.setup_two_endpoints())
     client, server = GreenMSRPSession(client), GreenMSRPSession(server)
     chunk = self.make_hello(client.msrp, success_report='yes')
     self._test_deliver_chunk(client, server, chunk)
     report = client.receive_chunk()
     self.assertEqual(report.method, 'REPORT')
     self.assertEqual(report.message_id, chunk.message_id)
     self.assertEqual(report.byte_range, chunk.byte_range)
     client.shutdown()
     server.shutdown()
示例#15
0
 def test_wait_error(self):
     def x():
         sleep(DELAY)
         return 1
     x = proc.spawn(x)
     z = proc.spawn(lambda : 3)
     y = proc.spawn(lambda : getcurrent().throw(ExpectedError('test_wait_error')))
     y.link(x)
     x.link(y)
     y.link(z)
     z.link(y)
     self.assertRaises(ExpectedError, proc.waitall, [x, y, z])
     self.assertRaises(proc.LinkedFailed, proc.waitall, [x])
     self.assertEqual(proc.waitall([z]), [3])
     self.assertRaises(ExpectedError, proc.waitall, [y])
示例#16
0
 def test_wait_error(self):
     def x():
         sleep(DELAY)
         return 1
     x = proc.spawn(x)
     z = proc.spawn(lambda : 3)
     y = proc.spawn(lambda : getcurrent().throw(ExpectedError('test_wait_error')))
     y.link(x)
     x.link(y)
     y.link(z)
     z.link(y)
     self.assertRaises(ExpectedError, proc.waitall, [x, y, z])
     self.assertRaises(proc.LinkedFailed, proc.waitall, [x])
     self.assertEqual(proc.waitall([z]), [3])
     self.assertRaises(ExpectedError, proc.waitall, [y])
示例#17
0
 def test_wait_error(self):
     def x():
         sleep(DELAY)
         return 1
     x = proc.spawn(x)
     z = proc.spawn(lambda : 3)
     y = proc.spawn(int, 'badint')
     y.link(x)
     x.link(y)
     y.link(z)
     z.link(y)
     self.assertRaises(ValueError, proc.waitall, [x, y, z])
     self.assertRaises(proc.LinkedFailed, proc.waitall, [x])
     self.assertEqual(proc.waitall([z]), [3])
     self.assertRaises(ValueError, proc.waitall, [y])
示例#18
0
文件: protocol.py 项目: esh/invaders
 def waitall(self):
     return proc.waitall(self.greenlets)
示例#19
0
Demonstrates how to use eventlet.green package and proc module.
"""
from eventlet import proc
from eventlet.green import socket

# this example works with both standard eventlet hubs and with twisted-based hub
# uncomment the following line to use twisted hub
#from twisted.internet import reactor

def geturl(url):
    c = socket.socket()
    ip = socket.gethostbyname(url)
    c.connect((ip, 80))
    print '%s connected' % url
    c.send('GET /\r\n\r\n')
    return c.recv(1024)

urls = ['www.google.com', 'www.yandex.ru', 'www.python.org']
jobs = [proc.spawn(geturl, x) for x in urls]
print 'spawned %s jobs' % len(jobs)

# collect the results from workers
results = proc.waitall(jobs)
# Note, that any exception in the workers will be reraised by waitall
# unless trap_errors argument specifies otherwise

for url, result in zip(urls, results):
    print '%s: %s' % (url, repr(result)[:50])