Ejemplo n.º 1
0
 def test_run(self):
     a = [0, 0]
     def task1():
         a[0] = 1
     def task2():
         a[1] = 1
     self.hub.schedule(greennet.greenlet(task1))
     self.hub.schedule(greennet.greenlet(task2))
     self.hub.run()
     self.assertEqual(a[0], 1)
     self.assertEqual(a[1], 1)
Ejemplo n.º 2
0
 def test_wait_until_empty_on_popleft(self):
     q = Queue(hub=self.hub)
     q.append('an item')
     q.append('another item')
     timeout = 0.5
     self.hub.call_later(greennet.greenlet(q.popleft), timeout)
     self.hub.call_later(greennet.greenlet(q.popleft), timeout * 2)
     start = time.time()
     q.wait_until_empty()
     duration = time.time() - start
     self.assert_(duration < timeout * 2 + IMMEDIATE_THRESHOLD
                  and duration > timeout * 2 - IMMEDIATE_THRESHOLD)
Ejemplo n.º 3
0
 def test_schedule(self):
     a = [0]
     def task():
         a[0] = 1
     self.hub.schedule(greennet.greenlet(task))
     self.hub.run()
     self.assertEqual(a[0], 1)
Ejemplo n.º 4
0
 def test_popleft_wait_for_append(self):
     q = Queue(hub=self.hub)
     timeout = 0.5
     self.hub.call_later(greennet.greenlet(q.append), timeout, 'an item')
     start = time.time()
     self.assertEqual(q.popleft(), 'an item')
     duration = time.time() - start
     self.assert_(duration < timeout + IMMEDIATE_THRESHOLD
                  and duration > timeout - IMMEDIATE_THRESHOLD)
Ejemplo n.º 5
0
 def test_schedule_with_args(self):
     a = [0]
     def task(arg1, arg2, *args):
         a.append(arg1)
         a.append(arg2)
         a.extend(args)
     self.hub.schedule(greennet.greenlet(task),
                       1, 2, *(3, 4))
     self.hub.run()
     self.assertEqual(a, [0, 1, 2, 3, 4])
Ejemplo n.º 6
0
 def accept_ssl(sock, application, cert, verify=None, timeout=None):
     with closing(sock):
         while True:
             client, addr = greennet.accept(sock)
             try:
                 client = greennet.ssl.accept(client, cert, verify, timeout)
             except greennet.Timeout:
                 client.close()
                 continue
             greennet.schedule(greenlet(handle_connection), client, application, True)
Ejemplo n.º 7
0
 def test_appendleft_wait_for_clear(self):
     q = Queue(1, hub=self.hub)
     q.append('an item')
     timeout = 0.5
     self.hub.call_later(greennet.greenlet(q.clear), timeout)
     start = time.time()
     q.appendleft('an item')
     duration = time.time() - start
     self.assert_(duration < timeout + IMMEDIATE_THRESHOLD
                  and duration > timeout - IMMEDIATE_THRESHOLD)
     self.assertEqual(q.pop(), 'an item')
Ejemplo n.º 8
0
 def test_call_later(self):
     a = [0]
     def task():
         a[0] = 1
     timeout = 0.5
     start = time.time()
     self.hub.call_later(greennet.greenlet(task), timeout)
     self.hub.run()
     duration = time.time() - start
     self.assert_(duration < timeout + IMMEDIATE_THRESHOLD
                  and duration > timeout - IMMEDIATE_THRESHOLD)
     self.assertEqual(a[0], 1)
Ejemplo n.º 9
0
 def test_switch(self):
     a = [0]
     def task():
         a[0] = 1
         self.hub.switch()
         self.assertEqual(a[0], 2)
         a[0] = 3
     self.hub.schedule(greennet.greenlet(task))
     self.hub.switch()
     self.assertEqual(a[0], 1)
     a[0] = 2
     self.hub.run()
     self.assertEqual(a[0], 3)
Ejemplo n.º 10
0
def serve(application, addr=('', 8000)):
    trigger = Trigger()
    import signal
    handler = signal_handler(trigger)
    signal.signal(signal.SIGINT, handler)
    sock = listen(addr)
    greennet.schedule(greenlet(stop), trigger, sock)
    greennet.switch()
    try:
        accept_connections(sock, application)
    except (socket.error, select.error), err:
        if err.args[0] != errno.EBADF:
            raise
Ejemplo n.º 11
0
 def test_call_later_with_args(self):
     a = [0]
     def task(arg1, arg2, *args):
         a.append(arg1)
         a.append(arg2)
         a.extend(args)
     timeout = 0.5
     start = time.time()
     self.hub.call_later(greennet.greenlet(task), timeout, 1, 2, *(3, 4))
     self.hub.run()
     duration = time.time() - start
     self.assert_(duration < timeout + IMMEDIATE_THRESHOLD
                  and duration > timeout - IMMEDIATE_THRESHOLD)
     self.assertEqual(a, [0, 1, 2, 3, 4])
Ejemplo n.º 12
0
 def _handle_timeouts(self):
     """Fire timeout events and return the next-expiring timeout.
     
     If there are no more timeouts, returns None.
     """
     while self.timeouts:
         wait = self.timeouts[0]
         timeout = wait.expires - time.time()
         if timeout <= 0.0:
             heapq.heappop(self.timeouts)
             if isinstance(wait, FDWait):
                 self.fdwaits.remove(wait)
             self.schedule(greenlet(wait.timeout))
             self._run_tasks()
         else:
             return timeout
Ejemplo n.º 13
0
 def __init__(self):
     self.greenlet = greenlet(self._run)
     self.fdwaits = set()
     self.timeouts = []
     self.tasks = deque()
Ejemplo n.º 14
0
def accept_connections(sock, application):
    with closing(sock):
        while True:
            client, addr = greennet.accept(sock)
            greennet.schedule(greenlet(handle_connection), client, application)