Ejemplo n.º 1
0
 def __init__(self, args):
     self._args = args
     self._authenticate(args)
     self._num_auths = 0
     self.semaphore = threading.Semaphore()
     self._queue = Queue()
Ejemplo n.º 2
0
    def test_consume_one_message_at_a_time(self):
        # end to end test
        #    - Process P1 is producing one message every 5 seconds
        #    - Process P2 is producing one other message every 3 seconds
        #    - Process S creates a auto-delete=False queue without a consumer and without a binding
        #    - Process S binds this queue through a pyon.net or container API call to the topic of process P1
        #    - Process S waits a bit
        #    - Process S checks the number of messages in the queue
        #    - Process S creates a consumer, takes one message off the queue (non-blocking) and destroys the consumer
        #    - Process S waits a bit (let messages accumulate)
        #    - Process S creates a consumer, takes a message off and repeates it until no messges are left (without ever blocking) and destroys the consumer
        #    - Process S waits a bit (let messages accumulate)
        #    - Process S creates a consumer, takes a message off and repeates it until no messges are left (without ever blocking). Then requeues the last message and destroys the consumer
        #    - Process S creates a consumer, takes one message off the queue (non-blocking) and destroys the consumer.
        #    - Process S sends prior message to its queue (note: may be tricky without a subscription to yourself)
        #    - Process S changes the binding of queue to P1 and P2
        #    - Process S removes all bindings of queue
        #    - Process S deletes the queue
        #    - Process S exists without any residual resources in the broker
        #    - Process P1 and P1 get terminated without any residual resources in the broker
        #
        #    * Show this works with the ACK or no-ACK mode
        #    * Do the above with semi-abstracted calles (some nicer boilerplate)

        def every_five():
            p = self.container.node.channel(PublisherChannel)
            p._send_name = NameTrio(bootstrap.get_sys_name(), 'routed.5')
            counter = 0

            while not self.publish_five.wait(timeout=1):
                p.send('5,' + str(counter))
                counter += 1

        def every_three():
            p = self.container.node.channel(PublisherChannel)
            p._send_name = NameTrio(bootstrap.get_sys_name(), 'routed.3')
            counter = 0

            while not self.publish_three.wait(timeout=0.6):
                p.send('3,' + str(counter))
                counter += 1

        self.publish_five = Event()
        self.publish_three = Event()
        self.five_events = Queue()
        self.three_events = Queue()

        gl_every_five = spawn(every_five)
        gl_every_three = spawn(every_three)

        def listen(lch):
            """
            The purpose of the this listen method is to trigger waits in code below.
            By setting up a listener that subscribes to both 3 and 5, and putting received
            messages into the appropriate gevent-queues client side, we can assume that
            the channel we're actually testing with get_stats etc has had the message delivered
            too.
            """
            lch._queue_auto_delete = False
            lch.setup_listener(
                NameTrio(bootstrap.get_sys_name(), 'alternate_listener'),
                'routed.3')
            lch._bind('routed.5')
            lch.start_consume()

            while True:
                try:
                    newchan = lch.accept()
                    m, h, d = newchan.recv()
                    count = m.rsplit(',', 1)[-1]
                    if m.startswith('5,'):
                        self.five_events.put(int(count))
                        newchan.ack(d)
                    elif m.startswith('3,'):
                        self.three_events.put(int(count))
                        newchan.ack(d)
                    else:
                        raise StandardError("unknown message: %s" % m)

                except ChannelClosedError:
                    break

        lch = self.container.node.channel(SubscriberChannel)
        gl_listen = spawn(listen, lch)

        def do_cleanups(gl_e5, gl_e3, gl_l, lch):
            self.publish_five.set()
            self.publish_three.set()
            gl_e5.join(timeout=5)
            gl_e3.join(timeout=5)

            lch.stop_consume()
            lch._destroy_queue()
            lch.close()
            gl_listen.join(timeout=5)

        self.addCleanup(do_cleanups, gl_every_five, gl_every_three, gl_listen,
                        lch)

        ch = self.container.node.channel(RecvChannel)
        ch._recv_name = NameTrio(bootstrap.get_sys_name(), 'test_queue')
        ch._queue_auto_delete = False

        # #########
        # THIS TEST EXPECTS OLD BEHAVIOR OF NO QOS, SO SET A HIGH BAR
        # #########
        ch._transport.qos_impl(prefetch_count=9999)

        def cleanup_channel(thech):
            thech._destroy_queue()
            thech.close()

        self.addCleanup(cleanup_channel, ch)

        # declare exchange and queue, no binding yet
        ch._declare_exchange(ch._recv_name.exchange)
        ch._declare_queue(ch._recv_name.queue)
        ch._purge()

        # do binding to 5 pub only
        ch._bind('routed.5')

        # wait for one message
        self.five_events.get(timeout=2)

        # ensure 1 message, 0 consumer
        self.assertTupleEqual((1, 0), ch.get_stats())

        # start a consumer
        ch.start_consume()
        time.sleep(0.2)
        self.assertEquals(
            ch._recv_queue.qsize(),
            1)  # should have been delivered to the channel, waiting for us now

        # receive one message with instant timeout
        m, h, d = ch.recv(timeout=0)
        self.assertEquals(m, "5,0")
        ch.ack(d)

        # we have no more messages, should instantly fail
        self.assertRaises(PQueue.Empty, ch.recv, timeout=0)

        # stop consumer
        ch.stop_consume()

        # wait until next 5 publish event
        num = self.five_events.get(timeout=2)
        self.assertEquals(num, 1)

        # start consumer again, empty queue
        ch.start_consume()
        time.sleep(0.1)
        while True:
            try:
                m, h, d = ch.recv(timeout=0)
                self.assertTrue(m.startswith('5,'))
                ch.ack(d)
            except PQueue.Empty:
                ch.stop_consume()
                break

        # wait for new message
        num = self.five_events.get(timeout=2)
        self.assertEquals(num, 2)

        # consume and requeue
        ch.start_consume()
        time.sleep(0.1)
        m, h, d = ch.recv(timeout=0)
        self.assertTrue(m.startswith('5,'))
        ch.reject(d, requeue=True)

        # rabbit appears to deliver this later on, only when we've got another message in it
        # wait for another message publish
        num = self.five_events.get(timeout=2)
        self.assertEquals(num, 3)
        time.sleep(0.1)

        expect = ["5,2", "5,3"]
        while True:
            try:
                m, h, d = ch.recv(timeout=0)
                self.assertTrue(m.startswith('5,'))
                self.assertEquals(m, expect.pop(0))

                ch.ack(d)
            except PQueue.Empty:
                ch.stop_consume()
                self.assertListEqual(expect, [])
                break

        # let's change the binding to the 3 now, empty the testqueue first (artifact of test)
        while not self.three_events.empty():
            self.three_events.get(timeout=0)

        # we have to keep the exchange around - it will likely autodelete.
        ch2 = self.container.node.channel(RecvChannel)
        ch2.setup_listener(NameTrio(bootstrap.get_sys_name(), "another_queue"))

        ch._destroy_binding()
        ch._bind('routed.3')

        ch2._destroy_queue()
        ch2.close()

        self.three_events.get(timeout=1)
        ch.start_consume()
        time.sleep(0.1)
        self.assertEquals(ch._recv_queue.qsize(), 1)

        m, h, d = ch.recv(timeout=0)
        self.assertTrue(m.startswith('3,'))
        ch.ack(d)

        # wait for a new 3 to reject
        self.three_events.get(timeout=10)
        time.sleep(0.1)

        m, h, d = ch.recv(timeout=0)
        ch.reject(d, requeue=True)

        # recycle consumption, should get the requeued message right away?
        ch.stop_consume()
        ch.start_consume()
        time.sleep(0.1)

        self.assertEquals(ch._recv_queue.qsize(), 1)

        m2, h2, d2 = ch.recv(timeout=0)
        self.assertEquals(m, m2)

        ch.stop_consume()
Ejemplo n.º 3
0
from gevent.event import AsyncResult
from gevent.queue import Empty, Queue
from gevent.timeout import Timeout
import flask

from analyzer import Analyzer
analyzer = Analyzer()

DATA_DIR = 'data'
KEEP_ALIVE_DELAY = 25
MAX_IMAGE_SIZE = 800, 600
MAX_IMAGES = 5
MAX_DURATION = 300

APP = flask.Flask(__name__, static_folder=DATA_DIR)
BROADCAST_QUEUE = Queue()

try:  # Reset saved files on each start
    rmtree(DATA_DIR, True)
    os.mkdir(DATA_DIR)
except OSError:
    pass


def broadcast(message):
    """Notify all waiting waiting gthreads of message."""
    waiting = []
    try:
        while True:
            waiting.append(BROADCAST_QUEUE.get(block=False))
    except Empty:
Ejemplo n.º 4
0
def get_all_foods(group_url):
    next_food = group_url
    while next_food:
        food_info, next_food = get_page_info(next_food)
        for food in food_info:
            food_name = food.find("h4").text.strip()
            food_link = base_url + food.find("h4").find("a")["href"]
            food_calorie = food.find("p").text.strip()

            print("食物名称:{}".format(food_name))
            print("食物链接:{}".format(food_link))
            print(food_calorie)
            print("-" * 30)


def do_jobs(event_queue):
    while not event_queue.empty():
        get_all_foods(event_queue.get_nowait())


food_url_queue = Queue()
for url in get_all_urls():
    food_url_queue.put_nowait(url)

task_list = []
for spider in range(5):
    task = gevent.spawn(do_jobs, food_url_queue)
    task_list.append(task)

gevent.joinall(task_list)
Ejemplo n.º 5
0

def application(environ, start_response):
    status = '200 OK'

    headers = [('Content-Type', 'text/html')]

    start_response(status, headers)
    yield "<p>Hello"
    yield "World</p>"


import gevent.monkey
gevent.monkey.patch_all(thread=False)
from gevent.queue import Queue
tasks = Queue()
co_queue = Queue()


def worker(n):
    print('Worker %s started' % n)
    try:
        while True:
            task = tasks.get()
            print('Worker %s got task %s' % (n, task))
            gevent.sleep(0)
    except:
        print('except..........')
    print('Quitting time!')

Ejemplo n.º 6
0
# little changes from: https://github.com/pglass/designate-locust/blob/master/graphite_client.py
import sys
import os
import time
import locust
import gevent

# from gevent.socket import socket
from gevent.queue import Queue

graphite_queue = Queue()
user_count_map = {}
HttpsLogServer = 'https://stag-events.sumologic.net/receiver/v1/http/ZaVnC4dhaV0ovzMt2yphbE2j8kzawgBL1WP7m_mon8BkoLwX5429XdmxGpKXGlhg62V0jMEOo7g61a6yUalUtrxyKXbLYpUa15mh_Q8CW-h9R3XMs8aH3A=='
# HOST = os.getenv('GRAPHITE_HOST', '')
# PORT = os.getenv('GRAPHITE_PORT', '443')


def is_slave():
    return '--slave' in sys.argv


def graphite_worker():
    """The worker pops each item off the queue and sends it to graphite."""
    # print('connecting to graphite on (%s, %s)' % (HOST, PORT))
    # sock = socket()
    # try:
    #     sock.connect((HOST, PORT))
    # except Exception as e:
    #     raise Exception(
    #         "Couldn't connect to Graphite server {0} on port {1}: {2}"
    #         .format(HOST, PORT, e))
Ejemplo n.º 7
0

def crawler(index):
    Process_id = 'Process-' + str(index)
    while not workQueue.empty():
        url = workQueue.get(timeout=2)
        try:
            r = requests.get(url, timeout=20)
            print(Process_id, workQueue.qsize(), r.status_code, url)
        except Exception as e:
            print(Process_id, workQueue.qsize(), url, 'Error: ', e)


def boss():
    for url in link_list:
        workQueue.put_nowait(url)


if __name__ == '__main__':
    workQueue = Queue(1000)

    gevent.spawn(boss).join()
    jobs = []
    for i in range(10):
        jobs.append(gevent.spawn(crawler, i))
    gevent.joinall(jobs)

    end = time.time()
    print('gevent + Queue多协程爬虫的总时间为:', end - start)
    print('Main Ended!')
Ejemplo n.º 8
0
    def __init__(self,
                 display_output=True,
                 serial_number="",
                 is_research=False):
        """
        Sets up initial values.
        """
        self.running = True
        self.packets = Queue()
        self.packets_received = 0
        self.packets_processed = 0
        self.battery = 0
        self.display_output = display_output
        self.is_research = is_research
        self.sensors = {
            'F3': {
                'value': 0,
                'quality': 0
            },
            'FC6': {
                'value': 0,
                'quality': 0
            },
            'P7': {
                'value': 0,
                'quality': 0
            },
            'T8': {
                'value': 0,
                'quality': 0
            },
            'F7': {
                'value': 0,
                'quality': 0
            },
            'F8': {
                'value': 0,
                'quality': 0
            },
            'T7': {
                'value': 0,
                'quality': 0
            },
            'P8': {
                'value': 0,
                'quality': 0
            },
            'AF4': {
                'value': 0,
                'quality': 0
            },
            'F4': {
                'value': 0,
                'quality': 0
            },
            'AF3': {
                'value': 0,
                'quality': 0
            },
            'O2': {
                'value': 0,
                'quality': 0
            },
            'O1': {
                'value': 0,
                'quality': 0
            },
            'FC5': {
                'value': 0,
                'quality': 0
            },
            'X': {
                'value': 0,
                'quality': 0
            },
            'Y': {
                'value': 0,
                'quality': 0
            },
            'Unknown': {
                'value': 0,
                'quality': 0
            }
        }

        self.serial_number = serial_number  # You will need to set this manually for OS X.
        self.old_model = False
        self.csv_name = 'holder1.csv'
        self.last_tme = 0
Ejemplo n.º 9
0
 def __init__(self, maxsize=8, **connectargs):
     self.maxsize = maxsize
     self.pool = Queue()
     self.lock = gevent.lock.BoundedSemaphore(maxsize)
     self._connectargs = connectargs
Ejemplo n.º 10
0
		finally:
			Lock.release()
	return

	
def Pre_fetch(HomePage_url, sess):
	query_dict = dict([(k,v[0]) for k,v in urlparse.parse_qs( urlparse.urlsplit(HomePage_url).query ).items()])
	Base = urlparse.urlsplit(HomePage_url)
	for i in range(1, (1+  web_parse.max_page_index(sess.get(HomePage_url).content))):
		query_dict["page"] = i
		yield urlparse.urlunparse(urlparse.ParseResult(
			scheme = Base.scheme,
			netloc = Base.netloc,
			path = Base.path,
			params = "",
			query = urllib.urlencode(query_dict),
			fragment = ""
		))
		
		
if __name__ == "__main__":
	Page_list = Queue()
	Forum_list = Queue()
	DB = db_handler.db_proc()
	Sess = requests.session()
	
	for i in Pre_fetch("https://www.t66y.com/thread0806.php?fid=8&search=&page=1", Sess):
		Page_list.put(i)
	print "Catch First Page Complete"
	gevent.joinall([gevent.spawn(forum_page_fetch, DB, Page_list, Forum_list, 0.5*i, Sess) for i in range(5)]+[gevent.spawn(Image_fetcher, DB, Page_list, Forum_list, 0.5*i, Sess) for i in range(20)])
Ejemplo n.º 11
0
 def __init__(self, maxsize=None, items=()):
     super().__init__()
     self._queue = Queue(maxsize, items)
Ejemplo n.º 12
0
 def __init__(self,
              displayOutput=False,
              headsetId=0,
              research_headset=True):
     self._goOn = True
     self.packets = Queue()
     self.packetsReceived = 0
     self.packetsProcessed = 0
     self.battery = 0
     self.displayOutput = displayOutput
     self.headsetId = headsetId
     self.research_headset = research_headset
     self.sensors = {
         'F3': {
             'value': 0,
             'quality': 0
         },
         'FC6': {
             'value': 0,
             'quality': 0
         },
         'P7': {
             'value': 0,
             'quality': 0
         },
         'T8': {
             'value': 0,
             'quality': 0
         },
         'F7': {
             'value': 0,
             'quality': 0
         },
         'F8': {
             'value': 0,
             'quality': 0
         },
         'T7': {
             'value': 0,
             'quality': 0
         },
         'P8': {
             'value': 0,
             'quality': 0
         },
         'AF4': {
             'value': 0,
             'quality': 0
         },
         'F4': {
             'value': 0,
             'quality': 0
         },
         'AF3': {
             'value': 0,
             'quality': 0
         },
         'O2': {
             'value': 0,
             'quality': 0
         },
         'O1': {
             'value': 0,
             'quality': 0
         },
         'FC5': {
             'value': 0,
             'quality': 0
         },
         'X': {
             'value': 0,
             'quality': 0
         },
         'Y': {
             'value': 0,
             'quality': 0
         },
         'Unknown': {
             'value': 0,
             'quality': 0
         }
     }
Ejemplo n.º 13
0
 def __init__(self):
     super(Task, self).__init__()
     self.response_queue = Queue()
Ejemplo n.º 14
0
    def __init__(self, scraperClass, gevent_num=100):
        self.scraperClass = scraperClass

        self.gevent_num = gevent_num
        self.tasks = Queue()
Ejemplo n.º 15
0
def transfer_queue():
    """Transfer event queue used by the confirmation sender."""
    return Queue()
Ejemplo n.º 16
0
Archivo: sentry.py Proyecto: fcua/x8623
 def __init__(self):
     if settings.SERVER_DSN:
         self.__client = raven.Client(
             dsn=settings.SERVER_DSN, transport=HTTPTransport)
         self.queue = Queue()
         self.loop = None
Ejemplo n.º 17
0
def pending_transaction_queue():
    return Queue()
Ejemplo n.º 18
0
    from gevent import monkey
    monkey.patch_all()
    from gevent.queue import Queue
    import gevent
    from fake_useragent import UserAgent
    ua = UserAgent()

    def list_to_queue(_):
        global queue
        for each in _:
            queue.put_nowait(each)
        return queue

    start = time()
    other_info = {'source_id': '1016533', 'city_id': '10067'}
    queue = Queue()
    url_list = (
        i for i in open(r'/Users/mioji/work/work_place/booking_task.txt', 'r'))
    # print url_list, '-----'
    url_list = [
        'http://www.booking.com/hotel/nl/salon-la-haye.zh-cn.html?aid=376390;label=misc-aHhSC9cmXHUO1ZtqOcw05wS94870954985%3Apl%3Ata%3Ap1%3Ap2%3Aac%3Aap1t1%3Aneg%3Afi%3Atikwd-11455299683%3Alp9061505%3Ali%3Adec%3Adm;sid=cf33ef00c3ac1c507f69fca9dbfcdbe8;checkin=2018-05-01;checkout=2018-05-02;ucfs=1;aer=1;srpvid=42bd1612c0660471;srepoch=1521601702;highlighted_blocks=100004801_101902367_2_0_0;all_sr_blocks=100004801_101902367_2_0_0;bshb=2;room1=A%2CA;hpos=10;hapos=205;dest_type=city;dest_id=-2147114;srfid=af747ee3d1c054c692eff026b7c1e77d5096aab2X205;from=searchresults;highlight_room=;spdest=ci/-2147114;spdist=22.7#hotelTmpl'
    ]
    gevent.spawn(list_to_queue, url_list).join()
    print(queue.qsize())

    def f(que):
        # print '抓取进来'
        while not que.empty():
            # print '循环来'
            url = que.get_nowait()
            url = url.replace('\\n', '')
Ejemplo n.º 19
0
 def reset(self):
     self._publish_queue = Queue()
Ejemplo n.º 20
0
    def __init__(self, url, protocols=None, extensions=None):
        WebSocketBaseClient.__init__(self, url, protocols, extensions)
        self._th = Greenlet(self.run)

        self.messages = Queue()
Ejemplo n.º 21
0
# encoding=utf8

import gevent
from gevent.queue import Queue

tasks = Queue()

def worker(n):
    while not tasks.empty():
        task = tasks.get()
        print 'worker %s got task %s' % (n, task)
        gevent.sleep(0)

    print 'Quitting time!'

def boss():
    for i in xrange(1, 25):
        tasks.put_nowait(i)

gevent.spawn(boss).join()

gevent.joinall([
    gevent.spawn(worker, 'steve'),
    gevent.spawn(worker, 'john'),
    gevent.spawn(worker, 'nancy'),
])
Ejemplo n.º 22
0
 def __init__(self, num_greenlets=50):
   super(GreenletExecutor, self).__init__()
   self.pool = Pool(size=num_greenlets)
   self.task_queue = Queue()
   self.num_ready = 0
Ejemplo n.º 23
0
start = time.time()

url_list = ['https://www.baidu.com/',
'https://www.sina.com.cn/',
'http://www.sohu.com/',
'https://www.qq.com/',
'https://www.163.com/',
'http://www.iqiyi.com/',
'http://www.ifeng.com/',
'https://www.qq.com/',
'https://www.163.com/',
'http://www.iqiyi.com/',
'https://www.tmall.com/',
'http://www.ifeng.com/']

work = Queue()

for url in url_list:
    work.put_nowait(url)

def crawler():
    while not work.empty():
        url = work.get_nowait()
        r = requests.get(url)
        print(url,work.qsize(),r.status_code)

tasks_list = []

for x in range(2):
    task = gevent.spawn(crawler)
    tasks_list.append(task)
Ejemplo n.º 24
0
 def test_imap_unordered_no_stop(self):
     q = Queue()
     q.put(1234)
     gevent.spawn_later(0.1, q.put, StopIteration)
     result = list(self.pool.imap_unordered(lambda _: _, q))
     self.assertEqual(result, [1234])
Ejemplo n.º 25
0
import gevent
from gevent.queue import Queue
 
products = Queue()
 
def consumer(name):
    while not products.empty():
        print '%s got product %s' % (name, products.get())
        gevent.sleep(0)
 
    print '%s Quit' % name
 
def producer():
    for i in xrange(1, 10):
        products.put(i)
 
gevent.joinall([
    gevent.spawn(producer),
    gevent.spawn(consumer, 'steve'),
    gevent.spawn(consumer, 'john'),
    gevent.spawn(consumer, 'nancy'),
])
Ejemplo n.º 26
0
    def on_transfer(self, transfer):
        """ This handles the echo logic, as described in
        https://github.com/raiden-network/raiden/issues/651:

            - for transfers with an amount that satisfies `amount % 3 == 0`, it sends a transfer
            with an amount of `amount - 1` back to the initiator
            - for transfers with a "lucky number" amount `amount == 7` it does not send anything
            back immediately -- after having received "lucky number transfers" from 7 different
            addresses it sends a transfer with `amount = 49` to one randomly chosen one
            (from the 7 lucky addresses)
            - consecutive entries to the lucky lottery will receive the current pool size as the
            `echo_amount`
            - for all other transfers it sends a transfer with the same `amount` back to the
            initiator """
        echo_amount = 0
        if transfer['amount'] % 3 == 0:
            log.debug('minus one transfer received',
                      initiator=pex(transfer['initiator']),
                      amount=transfer['amount'],
                      identifier=transfer['identifier'])
            echo_amount = transfer['amount'] - 1

        elif transfer['amount'] == 7:
            log.debug(
                'lucky number transfer received',
                initiator=pex(transfer['initiator']),
                amount=transfer['amount'],
                identifier=transfer['identifier'],
                poolsize=self.lottery_pool.qsize(),
            )

            # obtain a local copy of the pool
            pool = self.lottery_pool.copy()
            tickets = [pool.get() for _ in range(pool.qsize())]
            assert pool.empty()
            del pool

            if any(ticket['initiator'] == transfer['initiator']
                   for ticket in tickets):
                assert transfer not in tickets
                log.debug(
                    'duplicate lottery entry',
                    initiator=pex(transfer['initiator']),
                    identifier=transfer['identifier'],
                    poolsize=len(tickets),
                )
                # signal the poolsize to the participant
                echo_amount = len(tickets)

            # payout
            elif len(tickets) == 6:
                log.info('payout!')
                # reset the pool
                assert self.lottery_pool.qsize() == 6
                self.lottery_pool = Queue()
                # add new participant
                tickets.append(transfer)
                # choose the winner
                transfer = random.choice(tickets)
                echo_amount = 49
            else:
                self.lottery_pool.put(transfer)

        else:
            log.debug('echo transfer received',
                      initiator=pex(transfer['initiator']),
                      amount=transfer['amount'],
                      identifier=transfer['identifier'])
            echo_amount = transfer['amount']

        if echo_amount:
            log.debug('sending echo transfer',
                      target=pex(transfer['initiator']),
                      amount=echo_amount,
                      orig_identifier=transfer['identifier'],
                      echo_identifier=transfer['identifier'] + echo_amount,
                      token_address=pex(self.token_address),
                      num_handled_transfers=self.num_handled_transfers + 1)

            self.api.transfer_and_wait(transfer.registry_address,
                                       self.token_address,
                                       echo_amount,
                                       transfer['initiator'],
                                       identifier=transfer['identifier'] +
                                       echo_amount)
        self.num_handled_transfers += 1
Ejemplo n.º 27
0
 def __init__(self):
     self.task = Queue()
     self.res = Queue()
Ejemplo n.º 28
0
    def __call__(self, environ, start_response):
        urls = self.websocket.url_map.bind_to_environ(environ)
        try:
            endpoint, args = urls.match()
            handler = self.websocket.view_functions[endpoint]
        except HTTPException:
            handler = None

        if not handler or 'HTTP_SEC_WEBSOCKET_KEY' not in environ:
            return self.wsgi_app(environ, start_response)

        # do handshake
        uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'],
                                  environ.get('HTTP_ORIGIN', ''))

        # setup events
        send_event = Event()
        send_queue = Queue()

        recv_event = Event()
        recv_queue = Queue()

        # create websocket client
        client = self.client(environ, uwsgi.connection_fd(), send_event,
                             send_queue, recv_event, recv_queue,
                             self.websocket.timeout)

        # spawn handler
        def wrapped_handler():
            with self.websocket.app.request_context(environ):
                return handler(client, **args)

        handler = spawn(wrapped_handler)

        # spawn recv listener
        def listener(client):
            # wait max `client.timeout` seconds to allow ping to be sent
            select([client.fd], [], [], client.timeout)
            recv_event.set()

        listening = spawn(listener, client)

        while True:
            if not client.connected:
                recv_queue.put(None)
                listening.kill()
                handler.join(client.timeout)
                return ''

            # wait for event to draw our attention
            wait([handler, send_event, recv_event], None, 1)

            # handle send events
            if send_event.is_set():
                try:
                    while True:
                        uwsgi.websocket_send(send_queue.get_nowait())
                except Empty:
                    send_event.clear()
                except IOError:
                    client.connected = False

            # handle receive events
            elif recv_event.is_set():
                recv_event.clear()
                try:
                    message = True
                    # More than one message may have arrived, so keep reading
                    # until an empty message is read. Note that  select()
                    # won't register after we've read a byte until all the
                    # bytes are read, make certain to read all the data.
                    # Experimentally, not putting the final empty message
                    # into the queue caused websocket timeouts; theoretically
                    # this code can skip writing the empty message but clients
                    # should be able to ignore it anyway.
                    while message:
                        message = uwsgi.websocket_recv_nb()
                        recv_queue.put(message)
                    listening = spawn(listener, client)
                except IOError:
                    client.connected = False

            # handler done, we're outta here
            elif handler.ready():
                listening.kill()
                return ''
Ejemplo n.º 29
0
    return grequests.map(rs)

cProfile.run("haha(urls)")

def hehe(urls):
    hehe = [requests.get(i) for i in urls]
    return hehe

cProfile.run("hehe(urls)")


#队列
import gevent
from gevent.queue import Queue, Empty

tasks = Queue(maxsize=3)

def worker(n):
    try:
        while True:
            task = tasks.get(timeout=1) # decrements queue size by 1
            print('Worker %s got task %s' % (n, task))
            gevent.sleep(0)
    except Empty:
        print('Quitting time!')

def boss():
    """
    Boss will wait to hand out work until a individual worker is
    free since the maxsize of the task queue is 3.
    """
Ejemplo n.º 30
0
def queueFromFile(f, maxLineLength=None):
    q = Queue()
    gevent.spawn(copyFileToQueue, f, q, maxLineLength)
    return q