How to use the gevent.Greenlet function in gevent

To help you get started, we’ve selected a few gevent examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ceph / s3-tests / s3tests / common / greenlets.py View on Github external
import time

from ..common import context, get_next_key
from ..common.results import TransferGreenletResult
from ..realistic import FileVerifier


# Make sure context has somewhere to store what we need
context.update(bunch.Bunch(
    needs_first_read=collections.deque(),
    all_keys=[],
    files_iter=None,
))


class SafeTransferGreenlet(gevent.Greenlet):
    def __init__(self, timeout=120):
        gevent.Greenlet.__init__(self)
        self.timeout = timeout
        self.result = None

    def _run(self):
        result = self.result = TransferGreenletResult(self.type)
        result.markStarted()

        try:
            with gevent.Timeout(self.timeout, False):
                result.success = self._doit()
        except gevent.GreenletExit:
            return
        except:
            result.setError(show_traceback=True)
github mongodb / mongo-python-driver / test / test_pooling_base.py View on Github external
if use_request:
                cx.start_request()

            history.append('find_slow start')

            # Javascript function that pauses N seconds per document
            fn = delay(10)
            self.assertEqual(1, db.test.find({"$where": fn}).count())

            history.append('find_slow done')

            if use_request:
                cx.end_request()

        if self.use_greenlets:
            gr0, gr1 = Greenlet(find_slow), Greenlet(find_fast)
            gr0.start()
            gr1.start_later(.1)
        else:
            gr0 = threading.Thread(target=find_slow)
            gr0.setDaemon(True)
            gr1 = threading.Thread(target=find_fast)
            gr1.setDaemon(True)

            gr0.start()
            time.sleep(.1)
            gr1.start()

        gr0.join()
        gr1.join()

        self.assertEqual([
github initc3 / HoneyBadgerBFT-Python / test / test_timeouts.py View on Github external
def test_join():
    q1 = Queue(1)
    q2 = Queue(2)

    def _run(q, n):
        gevent.sleep(random.random()*2)
        q.put(n)

    Greenlet(_run, q1, 'Item 1').start()
    Greenlet(_run, q2, 'Item 2').start()

    tag, firstItem = joinQueues(q1, q2)

    print 'firstItem:', firstItem
    if tag == 'a':
        print 'waiting for Item2:'
        print q2.get()
    if tag == 'b':
        print 'waiting for Item1:'
        print q1.get()
github initc3 / HoneyBadgerBFT-Python / test / honest_party_test_tor_multipleCircuits.py View on Github external
s.setproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", TOR_SOCKSPORT[party], True)
        s.connect((hostname, port))
        retry = False
      except Exception, e:  # socks.SOCKS5Error:  # still no idea why socks over tor would always generate this error
        retry = True
        gevent.sleep(1)
        s.close()
        mylog('retrying (%s, %d) caused by %s...' % (hostname, port, str(e)) , verboseLevel=-2)
    q = Queue()
    def _handle():
        while True:
            obj = q.get()
            content = encode(obj)
            s.sendall(struct.pack('
github ptone / django-live / django_project / colorpicks / socket.py View on Github external
data['data'])
                        io.emit(chan + ":" +
                                data['action'],
                                list(data['data'])
                                )

        # we could filter our own ID out, so we don't subscribe to
        # ourselves. It would depend on whether you want to allow changes
        # made through other avenues to be reflected
        # if you don't filter, that means there is no way to avoid
        # getting your own round tripped updates - which defeates some of the
        # point of the client side MVC

        url = msg['url']
        if url not in self.subscribers:
            greenlet = Greenlet.spawn(subscriber, self, url)
            # stash this greenlet in a dictionary in order
            # to kill/unsub later on
            self.subscribers[url] = greenlet
        else:
            pass
            # dlog( 'already subscribed do ', url)
github jhgg / jeev / jeev / adapter / console.py View on Github external
def start(self):
        self._reader = Greenlet(self._read_stdin)
        self._stdin = FileObject(sys.stdin)
        self._stdout = FileObject(sys.stdout)
        self._reader.start()
github iterait / shepherd / cxworker / sheep / sheep.py View on Github external
def __init__(self, socket: zmq.Socket, sheep_data_root: str):
        self.config: Optional[self.Config] = None
        self.feeding_greenlet: gevent.Greenlet = None
        self.socket: zmq.Socket = socket
        self.requests_queue: Queue = Queue()
        self.requests_set: set = set()
        self.model_name: Optional[str] = None
        self.model_version: Optional[str] = None
        self.sheep_data_root: Optional[str] = sheep_data_root
github obulpathi / bitcoinpy / bitcoinpy / connection.py View on Github external
from gevent import Greenlet

import signal
import struct
import socket
import time
import sys
import hashlib
import log
import cStringIO

from common import *
from lib.coredefs import MIN_PROTO_VERSION
from lib.messages import messagemap, msg_version, msg_ping, message_to_str

class Connection(Greenlet):
    def __init__(self, node, peersocket, address, port = None):
        # for incoming connection, port = None
        # for outgoing conneciton, socket = None
        Greenlet.__init__(self)
        self.node = node
        self.socket = peersocket
        self.dstaddr = address
        self.dstport = port
        self.recvbuf = ""
        self.last_sent = 0
        self.getblocks_ok = True
        self.last_block_rx = time.time()
        self.last_getblocks = 0
        self.hash_continue = None
        self.log = log
        self.ver_recv = MIN_PROTO_VERSION
github initc3 / HoneyBadgerBFT-Python / mmr13 / includeTransaction.py View on Github external
def _listener():
        while True:
            # a = receive()
            # mylog(a, verboseLevel=-1)
            sender, (tag, m) = receive()
            # mylog("[%d] got a msg from %s\n %s" % (pid, repr(sender), repr((tag, m))), verboseLevel=-1)
            if tag == 'B':
                #mylog("[%d] CBChannel put %s" % (pid, repr((sender, m))))

                greenletPacker(Greenlet(CBChannel.put, (sender, m)),
                    'includeTransaction.CBChannel.put', (pid, N, t, setToInclude, broadcast, receive)).start()
            elif tag == 'A':
                greenletPacker(Greenlet(ACSChannel.put,
                    (sender, m)
                ), 'includeTransaction.ACSChannel.put', (pid, N, t, setToInclude, broadcast, receive)).start()
github initc3 / HoneyBadgerBFT-Python / experiments / mmr13_tor_multipleCircuits.py View on Github external
# Create the servers
    servers = []
    for i in range(N):
        _, port = TOR_MAPPINGS[i]
        servers.append(listen_to_channel(port))
    gevent.sleep(2)
    print 'servers started'


    ts = []
    for i in range(N):
        bc = makeBroadcast(i)
        recv = servers[i].get
        vi = inputs[i]
        th = Greenlet(mv84consensus, i, N, t, vi, bc, recv)
        th.start() # start_later(random.random() * maxdelay)
        ts.append(th)


    try:
        gevent.joinall(ts)
    except gevent.hub.LoopExit: # Manual fix for early stop
        agreed = ""
        for key, value in globalState.items():
            if globalState[key] != "":
                agreed = globalState[key]
        for key,  value in globalState.items():
            if globalState[key] == "":
                globalState[key] = agreed
            if globalState[key] != agreed:
                print "Consensus Error"