本文整理汇总了Python中multiprocessing.Pool类的典型用法代码示例。如果您正苦于以下问题:Python Pool类的具体用法?Python Pool怎么用?Python Pool使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Pool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start_crawlers
def start_crawlers(connector_class, num_processes=1):
"""
Starts a spider process for each spider class in the project
:param num_processes: the number of simultaneous crawling processes
:param connector_class: the connector class that should be used by the
spiders
"""
spider_classes = pyjobs_crawlers.tools.get_spiders_classes()
if num_processes == 0:
connector = connector_class()
with _get_lock('ALL') as acquired:
if acquired:
crawl(spider_classes, connector)
else:
print("Crawl process of 'ALL' already running")
return
# Splits the spider_classes list in x lists of size num_processes
spider_classes_chunks = list()
for x in range(0, len(spider_classes), num_processes):
spider_classes_chunks.append(spider_classes[x:x + num_processes])
# Start num_processes number of crawling processes
for spider_classes_chunk in spider_classes_chunks:
process_params_chunk = [(spider_class, connector_class)
for spider_class in spider_classes_chunk]
p = Pool(len(process_params_chunk))
p.map(start_crawl_process, process_params_chunk)
示例2: crawl_recursive_threaded
def crawl_recursive_threaded(dirpath, ext):
from database import indexer
from database import utils
from multiprocessing import Pool
# convert to our infos
cdir = indexer.DirInfo(dirpath, ext)
cInfos = indexer.dirs_to_info(cdir.subfolders(), ext)
# comment if you want a silent indexing
print(cdir.to_string())
# recursive pooled call
# NOTE: child calls must not be pooled
p = Pool(utils.Settings.config['processes'])
infos = p.map(crawl_recursive, cInfos)
p.close()
# remove hierarchy
dirInfos = [d for sublist in infos for d in sublist]
dirInfos.append(cdir)
print('I was crawling with %d processes' %
utils.Settings.config['processes'])
return dirInfos
示例3: rc
def rc(rf, alphabet, numOfThreads):
tryn=0
counterTmp = 0
printCounter = 1000
listBasic = []
if rf.endswith('.rar'):
funcChosen = unrar
elif rf.endswith('.zip') or rf.endswith('.7z') :
funcChosen = zipFileUnzip
for a in range(1,len(alphabet)+1):
for b in itertools.product(alphabet,repeat=a):
k="".join(b)
k=re.escape(k)
listBasic.append(k)
tryn+=1
if len(listBasic) == numOfThreads:
pool = Pool(numOfThreads)
pool.map_async(funcChosen, listBasic, callback = exitPass)
pool.close()
if resultPass:
timeWasted = time.time()-start
print 'Found! Password is '+resultPass
print "It took " +str(round(time.time()-start,3))+" seconds"
print "Speed: "+str(round(tryn/float(timeWasted),2))+" passwords/sec"
print "Tried "+str(tryn)+" passwords"
exit()
listBasic = []
counterTmp+=1
if counterTmp >= printCounter:
print 'Trying combination number '+str(tryn)+':'+str(k)
timeWasted = round(time.time()-start,2)
if timeWasted > 0:
print "It took already " +str(timeWasted) +" seconds. Speed: "+str(round(tryn/float(timeWasted),2))+" passwords/sec"
counterTmp=0
示例4: k_rbm
def k_rbm(infile, outfile):
#dataset
data = sio.loadmat(infile)['data']
# reconstruction cost
cost_dict = {}
p = Pool(5)
first_arg = ["Thread-1", "Thread-2", "Thread-3", "Thread-4", "Thread-5"]
second_arg = data
a,b,c,d,e = p.map(rbm_star, itertools.izip(first_arg, itertools.repeat(second_arg)))
# p.map(rbm_star, itertools.izip(first_arg, itertools.repeat(second_arg)))
# get the costs from the tuples
cost_1 = a[0]
cost_2 = b[1]
cost_3 = c[2]
cost_4 = d[3]
cost_5 = e[4]
# find the cluster assignments
for i in xrange(len(cost_1)):
mincost = min(cost_1[i],cost_2[i],cost_3[i],cost_4[i],cost_5[i])
if mincost == cost_1[i]:
cost_dict[i+1] = 1
elif mincost == cost_2[i]:
cost_dict[i+1] = 2
elif mincost == cost_3[i]:
cost_dict[i+1] = 3
elif mincost == cost_4[i]:
cost_dict[i+1] = 4
else:
cost_dict[i+1] = 5
# store results
json.dump(cost_dict, open(outfile, 'w'))
示例5: spawn_runpy
def spawn_runpy(cp, wait=60, cb=check_rst):
"as decorator to run job"
global WAITQ, RUNQ, CFG
pool = Pool(processes=CFG['MAXJOBS'])
while len(WAITQ) > 0 or len(RUNQ) > 0:
if len(RUNQ) <= CFG['MAXJOBS'] and len(WAITQ) > 0:
path, test = WAITQ.pop()
rst = pool.apply_async(call_runpy, (cp, path, test,))
RUNQ.append((rst, test, timeit.default_timer()))
else:
for r in RUNQ:
usec = float("%.2f" %(timeit.default_timer()-r[2]))
if r[0].successful:
print "[{0}] success used {1} usec".format(r[1], usec)
RUNQ.remove(r)
if cb:
cb(r[1], 'pass', usec)
else:
if usec > CFG['TIMEOUT']:
print "[{0}] unsuccess used timeout {1} usec".format(r[1], usec)
r[0].terminate()
if cb:
cb(r[1], 'fail', usec)
time.sleep(float(wait))
示例6: compress_file
def compress_file(self,corpus, np=4,separator=None):
"""
construct WLZW pattern out of a corpus, parallelism is an option
@param corpus - string, file path of the corpus
@param np - number of processes, if np = 1 the algorithm is run in serial
@param separator - the separator string to separate doc id and document. pass None if no doc id is given
@return set, the final set containing all frequent patterns
"""
#if only one process, no need for parallelization
if np==1:
return set(_compress_file((corpus,0,np,separator)))
p=Pool(processes=np)
l=[]
for i in range(0,np):
l.append((corpus,i,np,separator))
result=p.imap_unordered(_compress_file,l,1)
if np==1:
final_set=result.next()
else:
final_set=_union(result)
return final_set
示例7: get
def get(self):
mode = toAlpha3Code(self.get_argument('lang'))
text = self.get_argument('q')
if not text:
self.send_error(400, explanation='Missing q argument')
return
def handleCoverage(coverage):
if coverage is None:
self.send_error(408, explanation='Request timed out')
else:
self.sendResponse([coverage])
if mode in self.analyzers:
pool = Pool(processes=1)
result = pool.apply_async(getCoverage, [text, self.analyzers[mode][0], self.analyzers[mode][1]])
pool.close()
@run_async_thread
def worker(callback):
try:
callback(result.get(timeout=self.timeout))
except TimeoutError:
pool.terminate()
callback(None)
coverage = yield tornado.gen.Task(worker)
handleCoverage(coverage)
else:
self.send_error(400, explanation='That mode is not installed')
示例8: main
def main(path, out, cores):
"""
Compute contact energies for each pdb in path and write results to 'out'.
:param path: str
:param out: str
:param cores: int
:return: None
"""
# Find all pdbs in path
workload = []
for file in os.listdir(path):
if os.path.splitext(file)[1].lower() == ".pdb":
workload.append(file)
# Print few newlines to prevent progressbar from messing up the shell
print("\n\n")
# Compute energies
pool = Pool(processes=cores)
results = []
for (nr, pdb) in enumerate(workload):
updateprogress(pdb, nr / len(workload))
e = computecontactenergy(os.path.join(path, pdb), pool)
results.append((pdb, e))
pool.close()
# Make 100% to appear
updateprogress("Finished", 1)
# Store output
with open(out, "w") as handler:
handler.write("PDB,Energy in kcal/mol\n")
for pair in results:
handler.write("{},{}\n".format(*pair))
示例9: JobPool
class JobPool(object):
"""
Pool container.
"""
pool = None
message_queue = None
def __init__(self, max_instances=4):
self.message_queue = Queue()
self.pool = Pool(max_instances, execute_task, (self.message_queue,))
atexit.register(self.clear)
def add_analysis(self, analysis):
"""
Add analysis to the pool.
"""
analysis.set_started()
self.message_queue.put(analysis)
def clear(self):
"""
Pool cleanup.
"""
self.pool.terminate()
self.pool.join()
示例10: YaraJobPool
class YaraJobPool(object):
"""
Yara pool container.
"""
pool = None
message_queue = None
def __init__(self, max_instances=3):
self.message_queue = Queue()
self.pool = Pool(max_instances, execute_yara_task,
(self.message_queue,))
atexit.register(self.clear)
def add_yara_task(self, yara_task):
"""
Adds the yara task.
"""
self.message_queue.put(yara_task)
def clear(self):
"""
Pool cleanup.
"""
self.pool.terminate()
self.pool.join()
示例11: get_location
def get_location(self):
"""
Extracts the location of each pixel in the satellite image
"""
self.ncols = self.satellite_gdal.RasterXSize / 2
self.nrows = self.satellite_gdal.RasterYSize / 2
self.length_df = self.nrows * self.ncols
print 'Columns, rows', self.ncols, self.nrows
cols_grid, rows_grid = np.meshgrid(
range(0, self.ncols),
range(0, self.nrows))
self.cols_grid = cols_grid.flatten()
self.rows_grid = rows_grid.flatten()
print 'Checking the meshgrid procedure works'
# getting a series of lat lon points for each pixel
self.geotransform = self.satellite_gdal.GetGeoTransform()
print 'Getting locations'
self.location_series = np.array(parmap.starmap(
pixel_to_coordinates,
zip(self.cols_grid, self.rows_grid),
self.geotransform,
processes = self.processes))
print 'Converting to Points'
pool = Pool(self.processes)
self.location_series = pool.map(
point_wrapper,
self.location_series)
示例12: get_fractional_errors
def get_fractional_errors(R_star, L_star, P_c, T_c):
"""
Pass in "guess" conditions.
Will then calculate inward and outward errors,
Returns:
[Data array]
dY - over/undershoots (+/-, going outward)
[dx handled outside this]
"""
# R_star, L_star, P_c, T_c = x
P_c_0 = modelparameters.P_c # core pressure, [dyne cm^-2]
T_c_0 = modelparameters.T_c # core temperature, [K]
R_star_0 = modelparameters.R_star
L_star_0 = modelparameters.L_star
print ""
print "R: " + str(R_star / R_star_0)
print "L: " + str(L_star / L_star_0)
print "P: " + str(P_c / P_c_0)
print "T: " + str(T_c / T_c_0)
X = modelparameters.X
Y = modelparameters.Y
Z = modelparameters.Z
mu = modelparameters.mu
params = (X, Y, Z, mu)
M_star = modelparameters.M_star
m_fitting_point = modelparameters.m_fitting_point
pool = Pool(2)
outward_results = pool.apply_async(integrate.integrate_outwards,
[M_star, m_fitting_point, P_c, T_c, mu, X, Y, Z] )
inward_results = pool.apply_async(integrate.integrate_inwards,
[M_star, m_fitting_point, R_star, L_star, mu, X, Y, Z] )
m_outward, y_outward, infodict_outward = outward_results.get()
m_inward, y_inward, infodict_inward = inward_results.get()
dr = y_inward[-1,0] - y_outward[-1,0]
dl = y_inward[-1,1] - y_outward[-1,1]
dP = y_inward[-1,2] - y_outward[-1,2]
dT = y_inward[-1,3] - y_outward[-1,3]
dY = np.array([dr, dl, dP, dT])
print ''
print 'fractional errors:'
print "dR: " + str(dr / y_inward[-1,0])
print "dL: " + str(dl / y_inward[-1,1])
print "dP: " + str(dP / y_inward[-1,2])
print "dT: " + str(dT / y_inward[-1,3])
return dY
示例13: score_all_genes
def score_all_genes(self, graph, num_procs=1):
partial_score_gene = partial(score_gene, graph=graph, top_genes=self.top_genes)
p = Pool(num_procs)
result = p.map(partial_score_gene, list(self.vd.gene_names()))
p.close()
# convert them all to percentiles
cent_hist = numpy.array([x[1] for x in result if x[1] != -1])
nn_hist = numpy.array([x[2] for x in result if x[2] != -1])
batch = []
for gene, cent_score, nn_score in result:
# edge case: gene is a top gene
if gene in self.top_genes:
cent_perc = 1
nn_perc = 1
# edge case: gene isn't in network
elif cent_score == -1 or \
nn_score == -1:
cent_perc = 0
nn_perc = 0
else:
cent_perc = scipy.stats.percentileofscore(cent_hist, cent_score) / 100.0
nn_perc = 1 - scipy.stats.percentileofscore(nn_hist, nn_score) / 100.0
print "gene: %s\n c: %s\n c_p: %s\n n: %s\n n_p: %s" % \
(gene, cent_score, cent_perc, nn_score, nn_perc)
batch.append((cent_score, cent_perc, nn_score, nn_perc, gene))
self.vd._c.executemany("UPDATE genes SET cent_score = ?, cent_perc = ?, " \
"nn_score = ?, nn_perc = ? WHERE name = ?", batch)
self.vd._conn.commit()
示例14: main
def main():
parser = ArgumentParser(description="Speed up your SHA. A different hash style.")
parser.add_argument('-1', '--sha1', action='store_true')
parser.add_argument('-2', '--sha224', action='store_true')
parser.add_argument('-3', '--sha256', action='store_true')
parser.add_argument('-4', '--sha384', action='store_true')
parser.add_argument('-5', '--sha512', action='store_true')
parser.add_argument('-f', '--file', type=str, help="The path to the file")
if len(sys.argv) == 1:
parser.print_help()
return
global args
args = parser.parse_args()
hashtree = ''
big_file = open(args.file, 'rb')
pool = Pool(multiprocessing.cpu_count())
for chunk_hash in pool.imap(hashing, chunks(big_file)):
hashtree += chunk_hash + ":hash"
pool.terminate()
print(str(hashing(hashtree.encode('ascii'))))
示例15: main
def main():
"""
---------------------------------------------------------------------------
AUTHOR: Kyle Hernandez
EMAIL: [email protected]
Calculate the distribution of polymorphic RAD loci across site classes.
---------------------------------------------------------------------------
USAGE: python snp_locations.py gmatrix.tab file.gff out.tab n_threads
ARGUMENTS:
gmatrix.tab - Tab-delimited genotype matrix file of variant sites
file.gff - GFF file
out.tab - Output file of counts
n_threads - The number of threads to run
"""
# Load the GFF and SNP positions into dictionaries
load_gff()
intergenic = process_matrix()
# Map:
# Create a pool of n_threads workers and use them to process
# scaffolds separately
ch_vals = sorted(gff_dict.keys())
sys.stdout.write("Counting features...\n")
pool = Pool(processes=n_threads)
ct_list = pool.map(process_dicts, ch_vals)
# Reduce:
# Process the list of dicts
print_counts(intergenic, ct_list)