本文整理汇总了Python中eventlet.semaphore.Semaphore.acquire方法的典型用法代码示例。如果您正苦于以下问题:Python Semaphore.acquire方法的具体用法?Python Semaphore.acquire怎么用?Python Semaphore.acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类eventlet.semaphore.Semaphore
的用法示例。
在下文中一共展示了Semaphore.acquire方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
class MJPEGProxy:
def __init__(self, listen_address, connect_address):
self.log = logging.getLogger('MJPEGProxy')
self.connect_address = connect_address
self.listen_address = listen_address
self.clients = []
self.connection = None
self.header = None
self.sem = Semaphore(1)
def run(self):
self.log.info("Starting")
eventlet.spawn_n(self.proxy)
self.listen()
def listen(self):
server = eventlet.listen(self.listen_address)
while True:
connection, address = server.accept()
self.add_client(connection, address)
def proxy(self):
while True:
eventlet.sleep(0) # sem.release(); sem.acquire() doesn't yield?!
self.sem.acquire()
if len(self.clients) == 0:
if self.connection:
self.disconnect()
self.sem.release()
eventlet.sleep(0.1)
continue
self.sem.release()
data = ''
try:
data = self.connection.recv(1024)
except:
self.log.info("Timed out reading data from source.")
if (len(data) == 0):
self.log.info("No data recieved from source, forcing reconnect.");
self.disconnect()
data = self.connect()
for client in self.clients:
try:
client.send(data)
except socket.error, err:
self.clients.remove(client)
client.close()
self.log.info("Client %s disconnected: %s [clients: %s]", client, err, len(self.clients))
示例2: test_exceptionleaks
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
def test_exceptionleaks(self):
# tests expected behaviour with all versions of greenlet
def test_gt(sem):
try:
raise KeyError()
except KeyError:
sem.release()
hubs.get_hub().switch()
# semaphores for controlling execution order
sem = Semaphore()
sem.acquire()
g = eventlet.spawn(test_gt, sem)
try:
sem.acquire()
assert sys.exc_info()[0] is None
finally:
g.kill()
示例3: EntrypointWaiter
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
class EntrypointWaiter(InjectionProvider):
"""Helper for `entrypoint_waiter`
Injection to be manually (and temporarily) added to an existing container.
Takes an entrypoint name, and exposes a `wait` method, which will return
once the entrypoint has fired.
"""
def __init__(self, entrypoint):
self.name = '_entrypoint_waiter_{}'.format(entrypoint)
self.entrypoint = entrypoint
self.done = Semaphore(value=0)
def worker_teardown(self, worker_ctx):
provider = worker_ctx.provider
if provider.name == self.entrypoint:
self.done.release()
def wait(self):
self.done.acquire()
示例4: EntrypointWaiter
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
class EntrypointWaiter(DependencyProvider):
"""Helper for `entrypoint_waiter`
DependencyProvider to be manually (and temporarily) added to an existing
container. Takes an entrypoint name, and exposes a `wait` method, which
will return once the entrypoint has fired.
"""
class Timeout(Exception):
pass
def __init__(self, entrypoint):
self.attr_name = '_entrypoint_waiter_{}'.format(entrypoint)
self.entrypoint = entrypoint
self.done = Semaphore(value=0)
def worker_teardown(self, worker_ctx):
entrypoint = worker_ctx.entrypoint
if entrypoint.method_name == self.entrypoint:
self.done.release()
def wait(self):
self.done.acquire()
示例5: Coroutine
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
class Coroutine(object):
"""
This class simulates a coroutine, which is ironic, as greenlet actually
*is* a coroutine. But trying to use greenlet here gives nasty results
since eventlet thoroughly monkey-patches things, making it difficult
to run greenlet on its own.
Essentially think of this as a wrapper for eventlet's threads which has a
run and sleep function similar to old school coroutines, meaning it won't
start until told and when asked to sleep it won't wake back up without
permission.
"""
ALL = []
def __init__(self, func, *args, **kwargs):
self.my_sem = Semaphore(0) # This is held by the thread as it runs.
self.caller_sem = None
self.dead = False
started = Event()
self.id = 5
self.ALL.append(self)
def go():
self.id = eventlet.corolocal.get_ident()
started.send(True)
self.my_sem.acquire(blocking=True, timeout=None)
try:
func(*args, **kwargs)
# except Exception as e:
# print("Exception in coroutine! %s" % e)
finally:
self.dead = True
self.caller_sem.release() # Relinquish control back to caller.
for i in range(len(self.ALL)):
if self.ALL[i].id == self.id:
del self.ALL[i]
break
true_spawn(go)
started.wait()
@classmethod
def get_current(cls):
"""Finds the coroutine associated with the thread which calls it."""
return cls.get_by_id(eventlet.corolocal.get_ident())
@classmethod
def get_by_id(cls, id):
for cr in cls.ALL:
if cr.id == id:
return cr
raise RuntimeError("Coroutine with id %s not found!" % id)
def sleep(self):
"""Puts the coroutine to sleep until run is called again.
This should only be called by the thread which owns this object.
"""
# Only call this from its own thread.
assert eventlet.corolocal.get_ident() == self.id
self.caller_sem.release() # Relinquish control back to caller.
self.my_sem.acquire(blocking=True, timeout=None)
def run(self):
"""Starts up the thread. Should be called from a different thread."""
# Don't call this from the thread which it represents.
assert eventlet.corolocal.get_ident() != self.id
self.caller_sem = Semaphore(0)
self.my_sem.release()
self.caller_sem.acquire() # Wait for it to finish.
示例6: Pool
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
class Pool(object):
def __init__(self, min_size=0, max_size=4, track_events=False):
if min_size > max_size:
raise ValueError('min_size cannot be bigger than max_size')
self.max_size = max_size
self.sem = Semaphore(max_size)
self.procs = proc.RunningProcSet()
if track_events:
self.results = coros.queue()
else:
self.results = None
def resize(self, new_max_size):
""" Change the :attr:`max_size` of the pool.
If the pool gets resized when there are more than *new_max_size*
coroutines checked out, when they are returned to the pool they will be
discarded. The return value of :meth:`free` will be negative in this
situation.
"""
max_size_delta = new_max_size - self.max_size
self.sem.counter += max_size_delta
self.max_size = new_max_size
@property
def current_size(self):
""" The number of coroutines that are currently executing jobs. """
return len(self.procs)
def free(self):
""" Returns the number of coroutines that are available for doing
work."""
return self.sem.counter
def execute(self, func, *args, **kwargs):
"""Execute func in one of the coroutines maintained
by the pool, when one is free.
Immediately returns a :class:`~eventlet.proc.Proc` object which can be
queried for the func's result.
>>> pool = Pool()
>>> task = pool.execute(lambda a: ('foo', a), 1)
>>> task.wait()
('foo', 1)
"""
# if reentering an empty pool, don't try to wait on a coroutine freeing
# itself -- instead, just execute in the current coroutine
if self.sem.locked() and api.getcurrent() in self.procs:
p = proc.spawn(func, *args, **kwargs)
try:
p.wait()
except:
pass
else:
self.sem.acquire()
p = self.procs.spawn(func, *args, **kwargs)
# assuming the above line cannot raise
p.link(lambda p: self.sem.release())
if self.results is not None:
p.link(self.results)
return p
execute_async = execute
def _execute(self, evt, func, args, kw):
p = self.execute(func, *args, **kw)
p.link(evt)
return p
def waitall(self):
""" Calling this function blocks until every coroutine
completes its work (i.e. there are 0 running coroutines)."""
return self.procs.waitall()
wait_all = waitall
def wait(self):
"""Wait for the next execute in the pool to complete,
and return the result."""
return self.results.wait()
def waiting(self):
"""Return the number of coroutines waiting to execute.
"""
if self.sem.balance < 0:
return -self.sem.balance
else:
return 0
def killall(self):
""" Kill every running coroutine as immediately as possible."""
return self.procs.killall()
def launch_all(self, function, iterable):
"""For each tuple (sequence) in *iterable*, launch ``function(*tuple)``
in its own coroutine -- like ``itertools.starmap()``, but in parallel.
Discard values returned by ``function()``. You should call
``wait_all()`` to wait for all coroutines, newly-launched plus any
previously-submitted :meth:`execute` or :meth:`execute_async` calls, to
#.........这里部分代码省略.........
示例7: DTestQueue
# 需要导入模块: from eventlet.semaphore import Semaphore [as 别名]
# 或者: from eventlet.semaphore.Semaphore import acquire [as 别名]
#.........这里部分代码省略.........
with self.waitlock:
# Is test waiting?
if dt not in self.waiting:
continue
# OK, check dependencies
elif dt._depcheck(self.output):
# No longer waiting
self.waiting.remove(dt)
# Place test on the run list
with self.runlock:
self.runlist.add(dt)
# Spawn the test
self.th_count += 1
spawn_n(self._run_test, dt)
# Dependencies failed; check if state changed and add
# its dependents if so
elif dt.state is not None:
# No longer waiting
self.waiting.remove(dt)
# Check all its dependents. Note--not trying to
# remove duplicates, because some formerly
# unrunnable tests may now be runnable because of
# the state change
tests.extend(list(dt.dependents))
def _run_test(self, dt):
"""
Execute ``dt``. This method is meant to be run in a new
thread.
Once a test is complete, the thread's dependents will be
passed back to the spawn() method, in order to pick up and
execute any tests that are now ready for execution.
"""
# Acquire the thread semaphore
if self.sem is not None:
self.sem.acquire()
# Increment the simultaneous thread count
self.th_simul += 1
if self.th_simul > self.th_max:
self.th_max = self.th_simul
# Save the output and test relative to this thread, for the
# status stream
status.setup(self.output, dt)
# Execute the test
try:
dt._run(self.output, self.res_mgr)
except:
# Add the exception to the caught list
self.caught.append(sys.exc_info())
# Manually transition the test to the ERROR state
dt._result._transition(ERROR, output=self.output)
# OK, done running the test; take it off the run list
with self.runlock:
self.runlist.remove(dt)
# Now, walk through its dependents and check readiness
self._spawn(dt.dependents)
# All right, we're done; release the semaphore
if self.sem is not None:
self.sem.release()
# Decrement the thread count
self.th_simul -= 1
self.th_count -= 1
# If thread count is now 0, signal the event
with self.waitlock:
if len(self.waiting) == 0 and self.th_count == 0:
self.th_event.send()
return
# If the run list is empty, that means we have a cycle
with self.runlock:
if len(self.runlist) == 0:
for dt2 in list(self.waiting):
# Manually transition to DEPFAIL
dt2._result._transition(DEPFAIL, output=self.output)
# Emit an error message to let the user know what
# happened
self.output.info("A dependency cycle was discovered. "
"Please examine the dependency graph "
"and correct the cycle. The --dot "
"option may be useful here.")
# Now, let's signal our event
self.th_event.send()