本文整理汇总了Python中multiprocessing.Condition.release方法的典型用法代码示例。如果您正苦于以下问题:Python Condition.release方法的具体用法?Python Condition.release怎么用?Python Condition.release使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Condition
的用法示例。
在下文中一共展示了Condition.release方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
def __call__(self, cv_iterator, evaluator, fold_callback=None,
n_jobs=None):
"""
"""
condvar = Condition()
results = []
def _signal_cb(result):
condvar.acquire()
results.append(result)
condvar.notify()
condvar.release()
folds = list(cv_iterator)
pool, deferreds = self.async(folds, evaluator,
fold_callback=_signal_cb, n_jobs=n_jobs)
pool.close()
while len(results) < len(folds):
condvar.acquire()
condvar.wait()
fold_estimator, result = results[-1]
fold_callback(fold_estimator, result)
condvar.release()
pool.join()
return results
示例2: test_watch_directory
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
def test_watch_directory():
def _cleanup(path):
for f in listdir(path):
p = join(path, f)
if isdir(p):
rmtree(p)
elif f != '.nothing':
unlink(p)
sample_template = ''
sample_directory = dirname(realpath(__file__)) + '/sample/'
watch_directory = sample_directory + 'watch/'
render_directory = sample_directory + 'render/'
template_directory = sample_directory + 'templates/'
with open(template_directory + 'haml.tmpl', 'r') as f:
sample_template = f.read()
condition = Condition()
p = Process(target=reloader.watch_directory,
args=(watch_directory, render_directory, condition))
condition.acquire()
p.start()
condition.wait()
try:
with open(watch_directory + 'test.haml', 'w') as f:
f.write(sample_template)
subdir = watch_directory + 'test_subdir/'
try:
mkdir(subdir)
except OSError:
if not isdir(subdir):
raise
with open(subdir + 'test_two.haml', 'w') as f:
f.write(sample_template)
sleep(1)
assert_true(exists(render_directory + 'test.html'))
assert_true(exists(render_directory + 'test_subdir/test_two.html'))
except:
raise
finally:
condition.release()
p.terminate()
p.join()
sleep(1)
_cleanup(watch_directory)
_cleanup(render_directory)
示例3: SynchronizingBus
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
class SynchronizingBus(Bus):
def __init__(self, sync_delay=1):
Bus.__init__(self)
self.sync_delay = sync_delay
self.condition = Condition()
def start(self):
import time
time.sleep(self.sync_delay)
self.log("Releasing children")
self.condition.acquire()
self.condition.notify_all()
self.condition.release()
Bus.start(self)
示例4: TProcessPoolServer
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
class TProcessPoolServer(TServer):
"""
Server with a fixed size pool of worker subprocesses which service requests.
Note that if you need shared state between the handlers - it's up to you!
Written by Dvir Volk, doat.com
"""
def __init__(self, * args):
TServer.__init__(self, *args)
self.numWorkers = 10
self.workers = []
self.isRunning = Value('b', False)
self.stopCondition = Condition()
self.postForkCallback = None
def setPostForkCallback(self, callback):
if not callable(callback):
raise TypeError("This is not a callback!")
self.postForkCallback = callback
def setNumWorkers(self, num):
"""Set the number of worker threads that should be created"""
self.numWorkers = num
def workerProcess(self):
"""Loop around getting clients from the shared queue and process them."""
if self.postForkCallback:
self.postForkCallback()
while self.isRunning.value == True:
try:
client = self.serverTransport.accept()
self.serveClient(client)
except (KeyboardInterrupt, SystemExit):
return 0
except (Exception) as x:
logging.exception(x)
def serveClient(self, client):
"""Process input/output from a client for as long as possible"""
itrans = self.inputTransportFactory.getTransport(client)
otrans = self.outputTransportFactory.getTransport(client)
iprot = self.inputProtocolFactory.getProtocol(itrans)
oprot = self.outputProtocolFactory.getProtocol(otrans)
try:
while True:
self.processor.process(iprot, oprot)
except (TTransportException) as tx:
pass
except (Exception) as x:
logging.exception(x)
itrans.close()
otrans.close()
def serve(self):
"""Start a fixed number of worker threads and put client into a queue"""
#this is a shared state that can tell the workers to exit when set as false
self.isRunning.value = True
#first bind and listen to the port
self.serverTransport.listen()
#fork the children
for i in range(self.numWorkers):
try:
w = Process(target=self.workerProcess)
w.daemon = True
w.start()
self.workers.append(w)
except (Exception) as x:
logging.exception(x)
#wait until the condition is set by stop()
while True:
self.stopCondition.acquire()
try:
self.stopCondition.wait()
break
except (SystemExit, KeyboardInterrupt):
break
except (Exception) as x:
logging.exception(x)
self.isRunning.value = False
def stop(self):
self.isRunning.value = False
self.stopCondition.acquire()
self.stopCondition.notify()
self.stopCondition.release()
示例5: Condition
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
import time
from multiprocessing import Process, Condition
import os
cond = Condition()
def wait_condition():
cond.acquire()
cond.wait()
print '[%d] waked!' % os.getpid()
cond.release()
for i in range(3):
Process(target=wait_condition).start()
time.sleep(1)
print 'notify!'
cond.acquire()
cond.notify_all()
cond.release()
示例6: WaitableQueue
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
class WaitableQueue(Queue):
"""Queue that uses a semaphore to reliably count items in it"""
class Vacuum(ThreadLoop):
def __init__(self, q, l):
def callback():
q.wait_notempty(0.1)
while True:
try:
val = q.get(False)
l.append(val)
except Empty:
break
ThreadLoop.__init__(self, callback)
def __init__(self, maxsize=0):
self.cond_empty = Condition()
self.cond_notempty = Condition()
self._put_counter = Value('i', 0)
Queue.__init__(self, maxsize)
def put(self, obj, block=True, timeout=None):
Queue.put(self, obj, block, timeout)
self._put_counter.value += 1
if self.qsize() != 0:
self.cond_notempty.acquire()
try:
self.cond_notempty.notify_all()
finally:
self.cond_notempty.release()
@property
def put_counter(self):
return self._put_counter.value
def get(self, block=True, timeout=None):
ret = Queue.get(self, block, timeout)
if self.qsize() == 0:
self.cond_empty.acquire()
try:
self.cond_empty.notify_all()
finally:
self.cond_empty.release()
return ret
def wait_empty(self, timeout=None):
"""Wait for all items to be got"""
self.cond_empty.acquire()
try:
if self.qsize():
self.cond_empty.wait(timeout)
finally:
self.cond_empty.release()
def wait_notempty(self, timeout=None):
"""Wait for all items to be got"""
self.cond_notempty.acquire()
try:
if self.qsize() == 0:
self.cond_notempty.wait(timeout)
finally:
self.cond_notempty.release()
示例7: RWLock
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import release [as 别名]
class RWLock():
"""A Readers-Writer lock.
Allows for multiple readers or one writer. Writers will not starve.
Attributes:
for_reading (RWLock.ReadLock): A lock-like object with appropriate
`acquire`, `release`, `__enter__` and `__exit__` methods pointed
to the *read methods of the RWLock. Chiefly for use with the
`with` statement.
for_writing (RWLock.WriteLock): A lock-like object with appropriate
`acquire`, `release`, `__enter__` and `__exit__` methods pointed
to the *write methods of the RWLock. Chiefly for use with the
`with` statement.
"""
class ReadLock():
def __init__(self, rw):
self._rw = rw
self.acquire = rw.acquire_read
self.release = rw.release_read
def __enter__(self):
self.acquire()
def __exit__(self, exception_type, exception_value, traceback):
self.release()
class WriteLock():
def __init__(self, rw):
self._rw = rw
self.acquire = rw.acquire_write
self.release = rw.release_write
def __enter__(self):
self.acquire()
def __exit__(self, exception_type, exception_value, traceback):
self.release()
def __init__(self):
"""Initialises the RWLock."""
self._condition = Condition()
self._readers = Value(c_uint64, 0, lock=False)
self._writers_waiting = Value(c_uint64, 0, lock=False)
self.for_reading = self.ReadLock(self)
self.for_writing = self.WriteLock(self)
def acquire_read(self):
"""Acquire a read lock.
Blocks if a thread has acquired the write lock or is waiting to
acquire the write lock.
"""
with self._condition:
while self._writers_waiting.value:
self._condition.wait()
self._readers.value += 1
def release_read(self):
"""Release a read lock."""
with self._condition:
self._readers.value -= 1
if not self._readers.value:
self._condition.notify_all()
def acquire_write(self):
"""Acquire a write lock.
Blocks until there are no acquired read or write locks.
"""
self._condition.acquire()
self._writers_waiting.value += 1
while self._readers.value:
self._condition.wait()
self._writers_waiting.value -= 1
def release_write(self):
"""Release a write lock."""
self._condition.release()