当前位置: 首页>>代码示例>>Python>>正文


Python gevent.spawn方法代码示例

本文整理汇总了Python中gevent.spawn方法的典型用法代码示例。如果您正苦于以下问题:Python gevent.spawn方法的具体用法?Python gevent.spawn怎么用?Python gevent.spawn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gevent的用法示例。


在下文中一共展示了gevent.spawn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_two_callbacks

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_two_callbacks(self):
        response_greenlet1 = gevent.spawn(self.client.send_task, 'message1')
        response_greenlet2 = gevent.spawn(self.client.send_task, 'message2')

        gevent.joinall([response_greenlet1, response_greenlet2])

        self.assertEquals('message1::response', response_greenlet1.get())
        self.assertEquals('message2::response', response_greenlet2.get())

        spans = self.tracer.finished_spans()
        self.assertEquals(len(spans), 2)

        for span in spans:
            self.assertEquals(span.tags.get(tags.SPAN_KIND, None),
                              tags.SPAN_KIND_RPC_CLIENT)

        self.assertNotSameTrace(spans[0], spans[1])
        self.assertIsNone(spans[0].parent_id)
        self.assertIsNone(spans[1].parent_id) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:21,代码来源:test_gevent.py

示例2: submit

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def submit(self):
        span = self.tracer.scope_manager.active.span

        def task1():
            with self.tracer.scope_manager.activate(span, False):
                span.set_tag('key1', '1')

                def task2():
                    with self.tracer.scope_manager.activate(span, False):
                        span.set_tag('key2', '2')

                        def task3():
                            with self.tracer.scope_manager.activate(span,
                                                                    True):
                                span.set_tag('key3', '3')

                        gevent.spawn(task3)

                gevent.spawn(task2)

        gevent.spawn(task1) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:23,代码来源:test_gevent.py

示例3: test_main

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_main(self):
        def main_task():
            with self.tracer.start_active_span('parent'):
                tasks = self.submit_callbacks()
                gevent.joinall(tasks)

        gevent.spawn(main_task)
        gevent.wait(timeout=5.0)

        spans = self.tracer.finished_spans()
        self.assertEquals(len(spans), 4)
        self.assertNamesEqual(spans, ['task', 'task', 'task', 'parent'])

        for i in range(3):
            self.assertSameTrace(spans[i], spans[-1])
            self.assertIsChildOf(spans[i], spans[-1]) 
开发者ID:opentracing,项目名称:opentracing-python,代码行数:18,代码来源:test_gevent.py

示例4: spawn_greenlet

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def spawn_greenlet(self, func, args, kwargs, greenlet_name):
    """Returns a gevent.Greenlet which has been initialized with the correct
    greenlet-local-storage state.

    Args:
      * greenlet_name (str|None) - If non-None, assign this to the greenlet's
        name.
    """
    self.close_non_parent_step()

    to_run = [pgs._get_setter_on_spawn() for pgs in PerGreentletStateRegistry]

    current_step = self._step_stack[-1]
    def _runner():
      for fn in to_run:
        fn()
      try:
        return func(*args, **kwargs)
      finally:
        self.close_non_parent_step()
    ret = gevent.spawn(_runner)
    if greenlet_name is not None:
      ret.name = greenlet_name
    current_step.greenlets.append(ret)
    return ret 
开发者ID:luci,项目名称:recipes-py,代码行数:27,代码来源:engine.py

示例5: child_client_runner

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def child_client_runner(server_address):
    """I am executed in a child process.

    Run many HTTP clients, each in its own greenlet. Each HTTP client
        - establishes a TCP connection to the server running in the parent
        - sends an HTTP request through it
        - reads the HTTP response and validates the response body
    """

    def get():
        body = request.urlopen('http://%s:%s/' % server_address).read()
        assert body == DUMMY_PAYLOAD

    t0 = time.time()
    clients = [gevent.spawn(get) for _ in range(N_HTTP_CLIENTS)]

    # Wait until all `get()` greenlet instances have completed.
    gevent.joinall(clients)
    duration = time.time() - t0
    print('%s HTTP clients served within %.2f s.' % (N_HTTP_CLIENTS, duration)) 
开发者ID:jgehrcke,项目名称:gipc,代码行数:22,代码来源:wsgimultiprocessing.py

示例6: test_lock_out_of_context_single

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_lock_out_of_context_single(self):
        r, w = pipe()
        g = gevent.spawn(lambda r: r.get(), r)
        gevent.sleep(SHORTTIME)
        with raises(GIPCLocked):
            with r:
                pass
                # The context manager can't close `r`, as it is locked in `g`.
        g.kill(block=False)
        # Ensure killing via 'context switch', i.e. yield control to other
        # coroutines (otherwise the subsequent close attempt will fail with
        # `GIPCLocked` error).
        gevent.sleep(-1)
        # Close writer first. otherwise, `os.close(r._fd)` would block on Win.
        w.close()
        r.close() 
开发者ID:jgehrcke,项目名称:gipc,代码行数:18,代码来源:test_gipc.py

示例7: test_lock_out_of_context_pair_3

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_lock_out_of_context_pair_3(self):
        with raises(GIPCLocked):
            with pipe(True) as (h1, h2):
                gr1 = gevent.spawn(lambda h: h.get(), h1)
                gr2 = gevent.spawn(lambda h: h.get(), h2)
                gevent.sleep(SHORTTIME)
                # Context succeeds closing h2 writer, fails upon closing h2
                # reader. Proceeds closing h1 writer, succeeds, closes h1
                # reader and fails.
        assert not h2._reader._closed
        assert not h1._reader._closed
        assert h2._writer._closed
        assert h1._writer._closed
        gr1.kill(block=False)
        gr2.kill(block=False)
        gevent.sleep(-1)
        h2.close()
        h1.close() 
开发者ID:jgehrcke,项目名称:gipc,代码行数:20,代码来源:test_gipc.py

示例8: test_lock_out_of_context_pair_4

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_lock_out_of_context_pair_4(self):
        with raises(GIPCLocked):
            with pipe(True) as (h1, h2):
                # Write more to pipe than pipe buffer can hold
                # (makes `put` block when there is no reader).
                # Buffer is quite large on Windows.
                gw1 = gevent.spawn(lambda h: h.put(LONGERTHANBUFFER), h1)
                gw2 = gevent.spawn(lambda h: h.put(LONGERTHANBUFFER), h2)
                gevent.sleep(SHORTTIME)
                # Context fails closing h2 writer, succeeds upon closing h2
                # reader. Proceeds closing h1 writer, fails, closes h1
                # reader and succeeds.
        assert h2._reader._closed
        assert h1._reader._closed
        assert not h2._writer._closed
        assert not h1._writer._closed
        gw1.kill(block=False)
        gw2.kill(block=False)
        gevent.sleep(-1)
        h2.close()
        h1.close() 
开发者ID:jgehrcke,项目名称:gipc,代码行数:23,代码来源:test_gipc.py

示例9: test_whatever_1

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def test_whatever_1(self):
        """
        From a writing child, fire into the pipe. In a greenlet in the parent,
        receive one of these messages and return it to the main greenlet.
        Expect message retrieval (child process creation) within a certain
        timeout interval. Terminate the child process after retrieval.
        """
        with pipe() as (r, w):
            def readgreenlet(reader):
                with gevent.Timeout(SHORTTIME * 5, False) as t:
                    m = reader.get(timeout=t)
                    return m
            p = start_process(usecase_child_a, args=(w, ))
            # Wait for process to send first message:
            r.get()
            # Second message must be available immediately now.
            g = gevent.spawn(readgreenlet, r)
            m = r.get()
            assert g.get() == "SPLASH"
            p.terminate()
            p.join()
            assert p.exitcode == -signal.SIGTERM 
开发者ID:jgehrcke,项目名称:gipc,代码行数:24,代码来源:test_gipc.py

示例10: usecase_child_d

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def usecase_child_d(forthreader, backwriter):
    recvqueue = gevent.queue.Queue()
    def g_from_forthpipe_to_q(forthreader):
        while True:
            m = forthreader.get()
            recvqueue.put(m)
            if m == "STOP":
                break

    def g_from_q_to_backpipe(backwriter):
        while True:
            m = recvqueue.get()
            backwriter.put(m)
            if m == "STOP":
                break

    g1 = gevent.spawn(g_from_forthpipe_to_q, forthreader)
    g2 = gevent.spawn(g_from_q_to_backpipe, backwriter)
    g1.get()
    g2.get() 
开发者ID:jgehrcke,项目名称:gipc,代码行数:22,代码来源:test_gipc.py

示例11: run

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def run(target, timeout=1):
    '''Helper for running a thread

    Args:
        target (function): function to run on a thread
        timeout (int): how long to wait for target to return
    Returns:
        data: result of the function
    '''
    last = None
    done = False

    while not done:
        g = spawn(target)
        g.join(timeout)

        while not g.successful():
            yield StreamNone(last)

        last = g.value
        if last is None:
            done = True
        else:
            yield last 
开发者ID:timkpaine,项目名称:tributary,代码行数:26,代码来源:thread.py

示例12: testConcurrentSend

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def testConcurrentSend(self):
        with create_actor_pool(address=True, n_process=4, distributor=AdminDistributor(4),
                               backend='gevent') as pool:
            ref1 = pool.create_actor(DummyActor, 0)

            def ref_send(ref, rg):
                p = []
                for i in range(*rg):
                    p.append(gevent.spawn(ref.send, ('send', ref1, 'add_ret', i)))
                self.assertEqual([f.get() for f in p], list(range(*rg)))

            n_ref = 20

            refs = [pool.create_actor(DummyActor, 0) for _ in range(n_ref)]

            ps = []
            for i in range(n_ref):
                r = (i * 100, (i + 1) * 100)
                refx = refs[i]
                ps.append(gevent.spawn(ref_send, refx, r))

            [p.get() for p in ps] 
开发者ID:mars-project,项目名称:mars,代码行数:24,代码来源:test_gevent_pool.py

示例13: _wait_worker_ready

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def _wait_worker_ready(proc, resource_ref):
        worker_ips = []

        def waiter():
            check_time = time.time()
            while True:
                if not resource_ref.get_workers_meta():
                    gevent.sleep(0.1)
                    if proc.poll() is not None:
                        raise SystemError('Worker dead. exit code %s' % proc.poll())
                    if time.time() - check_time > 20:
                        raise TimeoutError('Check meta_timestamp timeout')
                    continue
                else:
                    break
            val = resource_ref.get_workers_meta()
            worker_ips.extend(val.keys())

        gl = gevent.spawn(waiter)
        gl.join()
        return worker_ips[0] 
开发者ID:mars-project,项目名称:mars,代码行数:23,代码来源:test_main.py

示例14: resolve_hostnames

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def resolve_hostnames(hostnames, timeout):
    """
    Do DNS resolution for a given list of hostnames

    This function uses gevent to resolve all the hostnames in *parallel*

    Args:
        hostnames (list): A list of strings
        timeout (int): The number of seconds to wait for resolution of **all** hostnames

    Returns:
        list: A list of (hostname, address) tuples in the same order as the input list of hostnames

    """
    assert validators.PanoptesValidators.valid_nonempty_iterable_of_strings(hostnames), u'hostnames should be a list'
    assert validators.PanoptesValidators.valid_nonzero_integer(timeout), u'timeout should be an int greater than zero'

    jobs = [gevent.spawn(wrap_errors(gaierror, socket.gethostbyname), host) for host in hostnames]
    gevent.joinall(jobs, timeout=timeout)
    addresses = [job.value if not isinstance(job.get(), gaierror) else None for job in jobs]
    results = [(hostnames[i], result) for i, result in enumerate(addresses)]
    return results 
开发者ID:yahoo,项目名称:panoptes,代码行数:24,代码来源:helpers.py

示例15: get_hostnames

# 需要导入模块: import gevent [as 别名]
# 或者: from gevent import spawn [as 别名]
def get_hostnames(ips, timeout):
    """
    Do DNS resolution for a given list of IPs

    Args:
        ips (list): A list of IPs
        timeout (int): The number of seconds to wait for resolution of **all** IPs

    Returns:
        list: A list of (address, hosname) tuples in the same order as the input list of IPs
    """
    assert validators.PanoptesValidators.valid_nonempty_iterable_of_strings(ips), u'ips should be a list'
    assert validators.PanoptesValidators.valid_nonzero_integer(timeout), u'timeout should be an int greater than zero'

    jobs = [gevent.spawn(wrap_errors((gaierror, herror), socket.gethostbyaddr), ip) for ip in ips]
    gevent.joinall(jobs, timeout=timeout)
    hostnames = [None if isinstance(job.get(), (gaierror, herror)) else job.value for job in jobs]
    results = {
        ips[i]: unknown_hostname(ips[i]) if ((not result) or
                                             (not result[0]) or
                                             result[0].startswith(u'UNKNOWN'))
        else result[0]
        for i, result in enumerate(hostnames)}
    return results 
开发者ID:yahoo,项目名称:panoptes,代码行数:26,代码来源:helpers.py


注:本文中的gevent.spawn方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。