本文整理汇总了Python中multiprocessing.Condition.notify方法的典型用法代码示例。如果您正苦于以下问题:Python Condition.notify方法的具体用法?Python Condition.notify怎么用?Python Condition.notify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Condition
的用法示例。
在下文中一共展示了Condition.notify方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TProcessPoolServer
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import notify [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()
示例2: IODeviceManager
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import notify [as 别名]
class IODeviceManager(Thread):
def __init__(self, a_device, a_kernel, std_in=StandardInput(), std_out=StandardOutput()):
Thread.__init__(self)
self.set_device(a_device)
self.set_kernel(a_kernel)
self.set_input(std_in)
self.set_output(std_out)
self.set_mutex(RLock())
self.set_queue(SoQueue())
self.device_is_in_use = Condition(self.get_mutex())
self.the_queue_is_empty = Condition(self.get_mutex())
def get_kernel(self):
return self.kernel
def set_kernel(self, a_kernel):
self.kernel = a_kernel
def set_input(self, a_input):
self.std_in = a_input
def get_input(self):
return self.std_in
def set_output(self, a_output):
self.std_out = a_output
def get_output(self):
return self.std_out
def get_mutex(self):
return self.mutex
def set_mutex(self, a_mutex):
self.mutex = a_mutex
def get_queue(self):
return self.queue
def set_queue(self, a_queue):
self.queue = a_queue
def set_device(self, a_device):
self.device = a_device
self.get_device().set_device_manager(self)
def get_device(self):
return self.device
def the_device_is_busy(self):
with self.get_mutex():
return not self.get_device().is_not_busy()
def send_to_device(self):
with self.device_is_in_use:
while self.the_device_is_busy():
self.device_is_in_use.wait()
with self.get_mutex():
self.get_device().set_pcb(self.get())
self.get_device().process_pcb()
def notify_that_the_device_is_not_in_use(self):
with self.device_is_in_use:
self.device_is_in_use.notify()
def put(self, a_pcb):
with self.the_queue_is_empty:
with self.get_mutex():
self.get_queue().add_pcb(a_pcb)
self.the_queue_is_empty.notify()
def get(self):
with self.get_mutex():
return self.get_queue().get_first()
def queue_is_empty(self):
return self.get_queue().is_empty()
def send_io_end_interruption(self, a_pcb):
self.get_kernel().get_irq_manager().handle(Irq(IO_END_INTERRUPT, a_pcb))
def run(self):
while True:
with self.the_queue_is_empty:
while self.queue_is_empty():
self.the_queue_is_empty.wait()
self.send_to_device()
示例3: solve
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import notify [as 别名]
def solve(max_level, goal, num_workers):
# prepare message queue shared with workers
tasks = Queue()
task_lock = Lock()
task_cv = Condition(lock=task_lock)
# create and start workers
workers = []
for i in range(0, num_workers):
solutions = set()
parent_conn, child_connn = Pipe()
worker = Process(target=run_worker,
args=(child_connn, goal, max_level, tasks,
task_lock, task_cv))
worker.start()
workers.append((worker, parent_conn))
# Find all possible sequences: [n0, n1, n2, ..., nM] (M=max_level)
# where nX is the number of binary operators so that
# '1 <n0 ops> 2 <n1 ops> 3 <n2 ops> ... M+1 <nM ops>' can be a valid
# Reverse Polish Notation. Key conditions are:
# 1. n0 + n1 + ... + nM = M
# 2. for any X, n0 + n1 + ... + nX <= X
# (Note that from condition #2 n0 is always 0.)
# We'll build the sequences in 'numops_list' below while exploring cases
# in a BFS-like (or DP-like) manner.
# This is a queue to maintain outstanding search results. Its each element
# is a tuple of 2 items: 'numops_list', 'total_ops'
# A tuple of (N, T) means:
# - N = [n0, n1, ..., nX]
# - T = sum(N)
# (Note that we don't necessarily have to keep T as it can be derived
# from N. But we do this for efficiency).
# The search is completed when len(N) reaches M (i.e., X=M-1) by appending
# the last item of nM = M - (n0 + n1 + ... + nX) = M - T (see condition #1).
tmp = [([0], 0)]
while tmp:
numops_list, total_ops = tmp.pop(0)
level = len(numops_list)
if level < max_level:
# Expand the sequence with all possible numbers of operators at
# the current level so we can explore the next level for each of
# them.
for i in range(0, level - total_ops + 1): # see condition #2
tmp.append((numops_list + [i], total_ops + i))
else:
# Found one valid RPN template. Pass it to workers and have them
# work on it.
numops_list.append(max_level - total_ops)
with task_lock:
tasks.put(numops_list)
task_cv.notify()
# Tell workers all data have been passed.
solutions = set()
with task_lock:
for _ in workers:
tasks.put(None)
task_cv.notify_all()
# Wait until all workers complete the tasks, while receiving any
# intermediate and last solutions. The received solutions may not
# necessarily be fully unique, so we have to unify them here, again.
# Received data of 'None' means the corresponding worker has completed
# its task.
# Note: here we assume all workers are reasonably equally active in
# sending data, so we simply perform blocking receive.
conns = set([w[1] for w in workers])
while conns:
for c in conns.copy():
worker_data = c.recv()
if worker_data is None:
conns.remove(c)
continue
for solution in worker_data:
if solution not in solutions:
solutions.add(solution)
# All workers have completed. Cleanup them and print the final unified
# results. If we are to show all expressions (i.e. goal is None), sort
# results by the expressions' values (listing integers followed by all
# non-integers, followed by 'divided by 0' cases.
for w in workers:
w[0].join()
if goal is None:
l = list(solutions)
l.sort(key=lambda x: (0, x[0]) if type(x[0]) == int else (1, str(x[0])))
for solution in l:
print('%s = %s' % (solution[1], str(solution[0])))
else:
for solution in solutions:
print(solution)
示例4: IOManager
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import notify [as 别名]
#.........这里部分代码省略.........
STDOUT_WRITER.flush()
answer = get_input()
if answer.lower() in (_("y"), _("yes")) or (
not answer and default
):
return True
elif answer.lower() in (_("n"), _("no")) or (
not answer and not default
):
return False
STDOUT_WRITER.write(_("Please answer with 'y(es)' or 'n(o)'.\n"))
@contextmanager
def capture(self):
self.capture_mode = True
self.captured_io = {
'stderr': "",
'stdout': "",
}
yield self.captured_io
self.capture_mode = False
@property
def child_parameters(self):
return (self.output_lock, self.output_queue, self.status_line_cleared)
def debug(self, msg):
self.output_queue.put({'msg': 'LOG', 'log_type': 'DBG', 'text': msg})
def job_add(self, msg):
self.output_queue.put({'msg': 'LOG', 'log_type': 'JOB_ADD', 'text': msg})
def job_del(self, msg):
self.output_queue.put({'msg': 'LOG', 'log_type': 'JOB_DEL', 'text': msg})
def stderr(self, msg):
self.output_queue.put({'msg': 'LOG', 'log_type': 'ERR', 'text': msg})
def stdout(self, msg):
self.output_queue.put({'msg': 'LOG', 'log_type': 'OUT', 'text': msg})
@contextmanager
def job(self, job_text):
self.job_add(job_text)
yield
self.job_del(job_text)
@property
@contextmanager
def lock(self):
with self.output_lock:
self.status_line_cleared.wait()
yield
def _print_thread(self):
assert self.parent_mode
while True:
if self.output_lock.acquire(False):
msg = self.output_queue.get()
if msg['log_type'] == 'QUIT':
break
if self.debug_mode and msg['log_type'] in ('OUT', 'DBG', 'ERR'):
msg['text'] = datetime.now().strftime("[%Y-%m-%d %H:%M:%S.%f] ") + msg['text']
if self.jobs and TTY:
self._write("\r\033[K")
if msg['log_type'] == 'OUT':
self._write(msg['text'] + "\n")
elif msg['log_type'] == 'ERR':
self._write(msg['text'] + "\n", err=True)
elif msg['log_type'] == 'DBG' and self.debug_mode:
self._write(msg['text'] + "\n")
elif msg['log_type'] == 'JOB_ADD' and TTY:
self.jobs.append(msg['text'])
elif msg['log_type'] == 'JOB_DEL' and TTY:
self.jobs.remove(msg['text'])
if self.jobs and TTY:
self._write("[status] " + self.jobs[0])
self.output_lock.release()
else: # someone else is holding the output lock
# the process holding the lock should now be waiting for
# us to remove any status lines present before it starts
# printing
if self.jobs and TTY:
self._write("\r\033[K")
self.status_line_cleared.notify()
# now we wait until the other process has finished and
# released the output lock
self.output_lock.acquire()
self.output_lock.release()
def shutdown(self):
assert self.parent_mode
self.output_queue.put({'msg': 'LOG', 'log_type': 'QUIT'})
self.thread.join()
def _write(self, msg, err=False):
write_to_stream(STDERR_WRITER if err else STDOUT_WRITER, msg)
if self.capture_mode:
self.captured_io['stderr' if err else 'stdout'] += msg
示例5: Cpu
# 需要导入模块: from multiprocessing import Condition [as 别名]
# 或者: from multiprocessing.Condition import notify [as 别名]
class Cpu(object):
def __init__(self):
self.pcb = None
self.__mutex = RLock()
self.__pcb_not_set = Condition(self.__mutex)
self.__mem_not_allocated = Condition(self.__mutex)
self.__round_robin_policy_on = False
def enable_round_robin(self, round_robin_quantum):
self.__round_robin_policy_on = True
self.__round_robin = RoundRobin(round_robin_quantum)
def pcb_not_set(self):
return self.__pcb_not_set
def set_kernel(self, kernel):
self.__kernel = kernel
def is_pcb_set(self):
return self.pcb != None
def set_current_pcb(self, pcb):
with self.__pcb_not_set:
self.pcb = pcb
self.__pcb_not_set.notify()
def reset_pcb(self):
self.pcb = None
def get_current_pcb(self):
return self.pcb
def __get_mem_manager(self):
return self.__kernel.get_mem_manager()
def __get_irq_manager(self):
return self.__kernel.get_irq_manager()
def fetch_decode_and_execute(self):
with self.__pcb_not_set:
while(not self.is_pcb_set()):
self.__pcb_not_set.wait()
with self.__mutex:
self.__fetch()
self.__decode()
self.__execute()
def __fetch(self):
pcb = self.get_current_pcb()
address = self.__get_mem_manager().current_instruction_address(pcb)
with self.__mem_not_allocated:
while self.__get_mem_manager().get(pcb,address) == None:
self.__mem_not_allocated.wait()
self.__current_instruction = self.__get_mem_manager().get(pcb, address )
def __decode(self):
self.__send_interruption_if_is_io()
self.__send_interruption_if_is_kill()
def __send_interruption_if_is_kill(self):
if(self.__current_instruction.is_kill_instruction()):
self.send_end()
def __send_interruption_if_is_io(self):
if(self.__current_instruction.is_io_instruction()):
self.send_io()
def __execute(self):
self.__execute_if_is_cpu_instruction()
def __execute_if_is_cpu_instruction(self):
if (self.__current_instruction.is_cpu_instruction()):
self.__current_instruction.run()
self.get_current_pcb().increment_pc()
def send_interruption(self, a_interruption):
self.__get_irq_manager().handle(Irq(a_interruption, self.get_current_pcb()))
def send_timeout(self):
self.send_interruption(TIMEOUT_INTERRUPT)
def send_end(self):
self.send_interruption(KILL_INTERRUPT)
def send_io(self):
self.send_interruption(IO_INTERRUPT)
def on_signal(self):
if self.__round_robin_policy_on:
self.__round_robin.handle_action(self)
else:
self.fetch_decode_and_execute()