Esempio n. 1
0
def connection_handler(client_socket: socket, address: Tuple[str, int]):
    '''This greenlet is spawned for each client that connects to the server'''

    logger.info('New connection from %s:%s' % address)

    try:
        output = Channel()
        cancel = Channel()
        r = gevent.spawn(reader, client_socket, output, cancel)
        w = gevent.spawn(writer, client_socket, output, cancel)
        gevent.joinall([r, w])
    finally:
        logger.info('Disconnected %s:%s' % address)
Esempio n. 2
0
def _test_atomic():
    # NOTE: Nested context by comma is not available in Python 2.6.
    # o -- No gevent.
    with lets.atomic():
        1 + 2 + 3
    # x -- gevent.sleep()
    with pytest.raises(AssertionError):
        with lets.atomic():
            gevent.sleep(0.1)
    # x -- gevent.sleep() with 0 seconds.
    with pytest.raises(AssertionError):
        with lets.atomic():
            gevent.sleep(0)
    # o -- Greenlet.spawn()
    with lets.atomic():
        gevent.spawn(gevent.sleep, 0.1)
    # x -- Greenlet.join()
    with pytest.raises(AssertionError):
        with lets.atomic():
            g = gevent.spawn(gevent.sleep, 0.1)
            g.join()
    # x -- Greenlet.get()
    with pytest.raises(AssertionError):
        with lets.atomic():
            g = gevent.spawn(gevent.sleep, 0.1)
            g.get()
    # x -- gevent.joinall()
    with pytest.raises(AssertionError):
        with lets.atomic():
            g = gevent.spawn(gevent.sleep, 0.1)
            gevent.joinall([g])
    # o -- Event.set(), AsyncResult.set()
    with lets.atomic():
        Event().set()
        AsyncResult().set()
    # x -- Event.wait()
    with pytest.raises(AssertionError):
        with lets.atomic():
            Event().wait()
    # x -- Event.wait()
    with pytest.raises(AssertionError):
        with lets.atomic():
            AsyncResult().wait()
    # x -- Channel.put()
    with pytest.raises(AssertionError):
        with lets.atomic():
            ch = Channel()
            ch.put(123)
    # o -- First Semaphore.acquire()
    with lets.atomic():
        lock = Semaphore()
        lock.acquire()
    # x -- Second Semaphore.acquire()
    with pytest.raises(AssertionError):
        with lets.atomic():
            lock = Semaphore()
            lock.acquire()
            lock.acquire()
    # Back to normal.
    gevent.sleep(1)
Esempio n. 3
0
    def cpu(self, amount, call_if_blocking):
        """Block until `amount` of cpu, in millicores, is available.

    Requesting 0 cpu will never block or wait.
    Requesting < 0 cpu will raise ValueError.
    Requesting > _millicores_max will acquire the full CPU.

    Args:

      * amount (int) - The amount of millicores to acquire before yielding. Must
        be positive or will raise ValueError. If this exceeds the maximum amount
        of millicores available on the system, this will instead acquire the
        system maximum.
      * call_if_blocking (None|func(amount_blocked_on)) - `cpu` will invoke this
        callback if we would end up blocking before yielding. This callback
        should only be used for reporting/diagnostics (i.e. it shouldn't raise
        an exception.)

    Yields control once the requisite amount of cpu is available.
    """
        if amount < 0:
            raise ValueError('negative cpu amount')

        if amount > self._millicores_max:
            amount = self._millicores_max

        if amount > 0 and (self._waiters
                           or self._millicores_available < amount):
            # we need some amount of cores AND
            # someone else is already waiting, or there aren't enough cores left.
            if call_if_blocking:
                call_if_blocking(amount - self._millicores_available)
            wake_me = Channel()
            self._waiters.append((amount, wake_me))
            wake_me.get()
            # At this point the greenlet that woke us already reserved our cores for
            # us, and we're free to go.
        else:
            # Just directly take our cores.
            assert self._millicores_available >= amount
            self._millicores_available -= amount

        try:
            yield
        finally:
            self._millicores_available += amount
            # We just added some resource back to the pot. Try to wake as many others
            # as we can before proceeding.

            to_wake, to_keep = [], []
            for waiting_amount, chan in self._waiters:
                if waiting_amount <= self._millicores_available:
                    to_wake.append(chan)
                    self._millicores_available -= waiting_amount
                else:
                    to_keep.append((waiting_amount, chan))
            self._waiters = to_keep
            for chan in to_wake:
                chan.put(None)
Esempio n. 4
0
    def set_links_timeout(self, link):
        # stuff that won't be touched
        event = AsyncResult()
        link(event)

        queue = Channel()
        link(queue.put)
        return event, queue
Esempio n. 5
0
 def handle_wsgi_request(self, env, start_response):
     ch = Channel()
     req = Request(ch,
                   env,
                   start_response,
                   content_type=self.default_content_type)
     self << ('handle', req)
     return response_stream(ch)
Esempio n. 6
0
 def __init__(self, output, parent=None, endput=None):
     super(tokizer, self).__init__()
     self._output = output
     self._endput = endput
     self.parent = parent
     self.q = Channel()
     self.init()
     self.start_job("job", self._job)
     self.job.link(self._end)
Esempio n. 7
0
 def test_link_to_channel(self):
     p1 = gevent.spawn(lambda: 101)
     p2 = gevent.spawn(lambda: 102)
     p3 = gevent.spawn(lambda: 103)
     q = Channel()
     p1.link(q.put)
     p2.link(q.put)
     p3.link(q.put)
     results = [q.get().get(), q.get().get(), q.get().get()]
     assert sorted(results) == [101, 102, 103], results
Esempio n. 8
0
def test_gevent1():
    global CH
    import gevent
    print gevent, gevent.__version__
    from gevent.queue import Channel
    from gevent import spawn, wait

    CH = Channel()

    start = time()
    spawn(channel_get_put)
    CH.put(True)
    spawn(channel_get_put)
    wait()
    return time() - start
Esempio n. 9
0
 def subscribe(ws):
     try:
         if not channels:
             return
         subscriptions = random.choice(channels)
         ch = Channel()
         subscriptions.append(ch)
         _log("feed ready! - added subscription " + str(len(subscriptions)))
         while True:
             #_log("getting")
             msg = ch.get()
             if not msg:
                 _log("nothing to send!")
                 continue
             ws.send(msg)
             #_log("sent!")
             gevent.sleep(0)
     except WebSocketError:
         _log("WebSocketError on feed! - retrying")
     return
Esempio n. 10
0
 def __init__(self):
     self.done = Channel()
Esempio n. 11
0
 def __init__(self, sock, addr):
     Endpoint.__init__(self, sock, addr)
     Greenlet.__init__(self)
     self.ctlcmds = Channel()
     self.gamedata = Gamedata(recording=True)