本文整理汇总了Python中multiprocessing.JoinableQueue.put方法的典型用法代码示例。如果您正苦于以下问题:Python JoinableQueue.put方法的具体用法?Python JoinableQueue.put怎么用?Python JoinableQueue.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.JoinableQueue
的用法示例。
在下文中一共展示了JoinableQueue.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def main(multiplier):
# Establish communication queues
tasks = JoinableQueue()
results = Queue()
# Start consumers
num_consumers = cpu_count() * multiplier
print 'Creating %d consumers' % num_consumers
consumers = [Consumer(tasks, results) for i in xrange(num_consumers)]
for w in consumers:
w.start()
fout = open(os.path.join(settings.PERSIST_DIR, 'doc_matrix_comparison.csv'), 'w', 0)
rw = ResultWriter(results, csv.writer(fout))
rw.start()
#num_docs = 801781
num_docs = 25
for i in xrange(num_docs):
tasks.put(Task(i))
# Add a poison pill for each consumer
for i in xrange(num_consumers):
tasks.put(None)
# Wait for all of the tasks to finish
tasks.join()
results.put('STOP')
示例2: SimpleSynergeticServer
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
class SimpleSynergeticServer(Process):
def __init__(self, authen_key):
Process.__init__(self)
self.__task_queue = JoinableQueue(1)
self.__return_queue = Queue(1)
self.serv = Listener(('', 40000), authkey=authen_key)
def run(self):
print 'Server Works'
copy_reg.pickle(types.MethodType, _reduce_method)
#Start the synergeticProcess in Deamon Mode
worker_p = SynergeticProcess(self.__task_queue, self.__return_queue)
worker_p.deamon = True
worker_p.start()
while True:
print 'wait for Client'
pool_conn = self.serv.accept()
print 'connection Client Accepted'
while True:
print 'in LOOP Simple Server'
#There is no need for task_id in this version
try:
print 'Try to recv MSG'
unpickled_msg = pool_conn.recv()
print 'MSG Reseved'
except Exception as e: # EOFError:
print 'Fail To Receive MSG:', e
break
if unpickled_msg[0] == 'MODULES':
self.import_mods( unpickled_msg[1] )
ret = 'MODULES-READY'
else:
self.__task_queue.put(unpickled_msg)
ret = self.__return_queue.get()
try:
print 'SEND RESPONCE'
try:
pool_conn.send( ret )
except EOFError:
print 'SENT TO POOL FAILD'
print 'RESPONCE SENT ', ret
except EOFError:
break
pool_conn.close()
def import_mods(self, mods_d):
for mod_name, mod_bytecode in mods_d.items():
try:
fobj = open(mod_name + ".pyc", 'wb')
except Exception as e:
print("Synergeticprocessing.SimpleServer --> Module file error: %s" % e)
else:
fobj.write( mod_bytecode )
finally:
fobj.close()
for mod in mods_d:
print 'blocking'
__import__( mod )
print 'imported ', mod
示例3: MMapPool
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
class MMapPool(object):
def __init__(self, n, mmap_size):
self.n = n
self.mmap_size = mmap_size
self.pool = [mmap.mmap(-1, mmap_size) for _ in range(n)]
self.free_mmaps = set(range(n))
self.free_queue = JoinableQueue()
def new(self):
if not self.free_mmaps:
self.free_mmaps.add(self.free_queue.get())
self.free_queue.task_done()
while True:
try:
self.free_mmaps.add(self.free_queue.get_nowait())
self.free_queue.task_done()
except Empty:
break
mmap_idx = self.free_mmaps.pop()
return mmap_idx, self.pool[mmap_idx]
def join(self):
while len(self.free_mmaps) < self.n:
self.free_mmaps.add(self.free_queue.get())
self.free_queue.task_done()
def get(self, idx):
return self.pool[idx]
def free(self, idx):
self.free_queue.put(idx)
示例4: get_citations
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def get_citations(**args):
"""
Method to prepare the actual citation dictionary creation
"""
# create the queues
tasks = JoinableQueue()
results = JoinableQueue()
# how many threads are there to be used
if 'threads' in args:
threads = args['threads']
else:
threads = cpu_count()
# initialize the "harvesters" (each harvester get the citations for a bibcode)
harvesters = [ CitationHarvester(tasks, results) for i in range(threads)]
# start the harvesters
for b in harvesters:
b.start()
# put the bibcodes in the tasks queue
for bib in args['bibcodes']:
tasks.put(bib)
# add some 'None' values at the end of the tasks list, to faciliate proper closure
for i in range(threads):
tasks.put(None)
tasks.join()
for b in harvesters:
b.join()
return [item for sublist in cit_dict.values() for item in sublist]
示例5: aggress
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def aggress(map):
global startMap
startMap = map
#print "Regressing..."
state = State()
jobs = []
longestSolution = Value('d', 20)
highestScore = Value('d', 0)
queue = JoinableQueue()
manager = Manager()
d = manager.dict()
d.clear()
l = RLock()
if multiProc:
queue.put((state, map, 1))
for i in range(numProcs):
p = Process(target = multiMain, args=(startMap, l, d, queue,highestScore))
p.start()
queue.join()
else:
a(l, highestScore, d, None, state, map, 1)
示例6: __init__
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
class QueueTask:
def __init__(self):
self.queue = JoinableQueue()
self.event = Event()
atexit.register( self.queue.join )
process = Process(target=self.work)
process.daemon = True
process.start()
def work(self):
while True:
func, args, wait_for = self.queue.get()
for evt in wait_for:
evt.wait()
func(*args)
self.event.set()
self.queue.task_done()
def enqueue(self, func, args=[], wait_for=[]):
self.event.clear()
self.queue.put( (func, args, wait_for) )
return self.event
示例7: evaluate
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def evaluate(points,meshToBasis,kernel,quadRule,coeffs,nprocs=None):
"""Evaluate a kernel using the given coefficients"""
if nprocs==None: nprocs=cpu_count()
inputQueue=JoinableQueue()
nelements=meshToBasis.nelements
for elem in meshToBasis: inputQueue.put(elem)
buf=sharedctypes.RawArray('b',len(points[0])*numpy.dtype(numpy.complex128).itemsize)
result=numpy.frombuffer(buf,dtype=numpy.complex128)
result[:]=numpy.zeros(1,dtype=numpy.complex128)
time.sleep(.5)
workers=[]
for id in range(nprocs):
worker=EvaluationWorker(points,kernel,quadRule,coeffs,inputQueue,result)
worker.start()
workers.append(worker)
inputQueue.join()
for worker in workers: worker.join()
return result.copy()
示例8: __init__
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
class JavaMultipleParserExecutor:
def __init__(self, output_dir, repo_path, processes=None):
self.target_blobs = JoinableQueue()
self.num_consumers = processes if processes else cpu_count()
self.consumers = [JavaConsumer(self.target_blobs, repo_path, output_dir)
for i in range(self.num_consumers)]
for consumer in self.consumers:
consumer.start()
self.closed = False
def parse_blob(self, blob):
if self.closed:
return
self.target_blobs.put(blob.hexsha)
def join(self):
if self.closed:
return
for i in range(self.num_consumers):
self.target_blobs.put(None)
self.target_blobs.join()
self.closed = True
示例9: FlightProducer
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
class FlightProducer(Process):
def __init__(self, options={}, date_group=[]):
self.options = options
self.date_group = date_group
self.date_queue = JoinableQueue()
def start(self):
consumers_list = []
consumers_num = cpu_count() * 2
# Consumers
for i in xrange(consumers_num):
consumers_list.append(FlightConsumer(self.options, self.date_queue))
for consumer in consumers_list:
consumer.start()
# Put each date group to queue
for date_item in self.date_group:
self.date_queue.put(date_item)
# Tell consumers can exit
for i in xrange(consumers_num):
self.date_queue.put(None)
# Wait for all of the consumers to finish
self.date_queue.join()
print('Done')
示例10: launch_mesos_tf
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def launch_mesos_tf(marathon_url_str, tsknom_str, cpu_float, mem_float, ntasks_int, uri_str, marathon_usr, marathon_usrpwd, localhost_str, mxattempts=10):
toret_nodes = dict()
docker = False
if uri_str.find('docker') > -1:
uri_str = uri_str.replace('docker://', '')
docker = True
uri_str = uri_str.rstrip('/')
marathon_url_str = marathon_url_str.rstrip('/')
counter = 0
tq = JoinableQueue()
q = Queue()
plist = list()
consumers = [ Consumer(tq, q) for i in xrange(ntasks_int) ]
for c in consumers:
c.start()
for i in xrange(ntasks_int):
tq.put(Task(post_marathon_tasks, (marathon_url_str, tsknom_str, cpu_float, mem_float, i+1, ntasks_int, uri_str, marathon_usr, marathon_usrpwd, localhost_str, mxattempts, docker)))
for i in xrange(ntasks_int):
tq.put(None)
tq.join()
for i in xrange(1, ntasks_int+1):
toret_nodes[i] = q.get()
return toret_nodes
示例11: main
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def main(opts, files):
if opts.threads == 1:
log.info("running synchronously")
run_syncronous(opts, files)
else:
Q = JoinableQueue()
workers = [Worker(Q, opts) for i in xrange(opts.threads)]
log.info("initializing %d threads" % opts.threads)
for w in workers:
w.start()
# push log events onto the queue
events_iter = events(files, opts)
if opts.limit:
events_iter = itertools.islice(events_iter, opts.limit)
for event in events_iter:
Q.put(event)
# add poison pills
for i in xrange(opts.threads):
Q.put(None)
Q.join()
log.info("work complete. shutting down threads.")
for w in workers:
w.join()
示例12: queue_info
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def queue_info(iters=None,):
work = JoinableQueue()
for filename in iters:
work.put(obj=filename)
time.sleep(1)
return work
示例13: main
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def main():
jobs = JoinableQueue()
result = JoinableQueue()
numToProcess = -1
scores = pd.DataFrame(columns=['query','fmeasure','precision','recall',
'size','maxDistance','topHits',"contextSteps"])
print len(datasets)
for key in datasets:
jobs.put(key)
processed_count = Counter()
for i in xrange(NUMBER_OF_PROCESSES):
p = Process(target=work, args=(i, jobs, result, processed_count))
p.daemon = True
p.start()
#work(1, jobs, result, processed_count)
automated_annotations = {}
distances = {}
jobs.join()
dataset_index = collections.defaultdict(set)
annotated_datasets = set()
while not result.empty():
dataset, classes = result.get()
if len(classes) == 0:
annotated_datasets.add(dataset)
for c in classes.keys():
dataset_index[c].add(dataset)
owl_class = Class(c, graph=graph)
for parent in owl_class.parents:
dataset_index[parent.identifier].add(dataset)
result.task_done()
print '\n'
for query, c in queries.items():
manual = ground_truth[query]
automated = dataset_index[c]
hits = manual & automated
misses = manual - automated
precision = np.nan if len(automated) == 0 else float(len(hits)) / len(automated)
recall = np.nan if len(manual) == 0 else float(len(hits)) / len(manual)
if precision != 0 or recall != 0:
fmeasure = 0 if np.isnan(precision) or np.isnan(recall) else 2 * (precision * recall) / (precision + recall)
else:
fmeasure = 0
scores = scores.append(dict(query=query, size=len(manual), precision=precision, recall=recall, fmeasure=fmeasure,topHits=topHits, maxDistance=maxDistance, contextSteps = context_steps),
ignore_index=True)
print "Hits for", query, c
print '\n'.join(sorted(hits))
print scores
print "Annotated", len(annotated_datasets), "datasets."
示例14: setup_queue
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def setup_queue(options):
probe_servers = Queue()
progress_queue = Queue()
run = Probe.ProbeRun.objects.get(id = options.run_id)
summary_top = Results.ResultSummaryList.objects.get(part_of_run=run)
summary_top.setup()
connection.close()
threads = []
for i in range(options.threads):
new_thread = Process(target=SetupQueueThread, args=(i,run, probe_servers, progress_queue))
new_thread.daemon = True
new_thread.start()
threads.append(new_thread)
progress_thread = threading.Thread(target=__ProgressCounter, args=(run, progress_queue, threads,options))
progress_thread.daemon = True
progress_thread.start()
i = 0;
if options.input_filename and (not options.count or i < options.count):
for hostname_line in fileinput.input(options.input_filename, openhook=fileinput.hook_compressed):
probe_servers.put(hostname_line)
i+=1
if options.count and i >= options.count:
break;
probe_servers.join()
progress_queue.join()
return run
示例15: main
# 需要导入模块: from multiprocessing import JoinableQueue [as 别名]
# 或者: from multiprocessing.JoinableQueue import put [as 别名]
def main(workers=10):
"""
Executes main function of mini-framework's Control thread.
:param workers: Integer detailing number of worker FIFO threads to employ
"""
start_logging()
log_info("New multiprocessing session with {} workers".format(workers))
# Input JoinableQueue and Output Queue
inq = JoinableQueue(maxsize=int(workers*1.5))
outq = Queue(maxsize=int(workers*1.5))
ot = OutThread(workers, outq)
ot.start()
for _ in range(workers):
w = WorkerThread(inq, outq)
w.start()
# Create a sequence of a 1000 random alphabetic characters
random_chars = (ascii_letters[randint(0, 51)] for _ in range(1000))
# Keep input queue loaded for as long as possible
# Feed the process pool with work units
for work in enumerate(random_chars):
inq.put(work)
# Fill the input queue with Nones to shut the worker threads down
# which terminates the process pool
for _ in range(workers):
inq.put(None)
inq.join()
print("Control process terminating")