本文整理匯總了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)
示例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)
示例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])
示例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
示例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))
示例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()
示例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()
示例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()
示例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
示例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()
示例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
示例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]
示例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]
示例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
示例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