日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python多核运行程序怎么关闭_在多核上运行程序

發布時間:2023/12/19 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python多核运行程序怎么关闭_在多核上运行程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在用Python運行一個程序,使用線程來并行執行任務。這個任務是簡單的字符串匹配,我要將大量的短字符串匹配到數據庫中的長字符串。當我試圖將它并行化時,我決定將短字符串列表拆分為若干個子列表,這些子列表與核心數量相等,并分別在不同的核心上運行它們。但是,當我在5個或10個內核上運行任務時,它比僅在一個內核上運行要慢兩倍。原因是什么?我怎么可能解決它?

編輯:我的代碼可以在下面看到import sys

import os

import csv

import re

import threading

from Queue import Queue

from time import sleep

from threading import Lock

q_in = Queue()

q_out = Queue()

lock = Lock()

def ceil(nu):

if int(nu) == nu:

return int(nu)

else:

return int(nu) + 1

def opencsv(csvv):

with open(csvv) as csvfile:

peptides = []

reader = csv.DictReader(csvfile)

k = 0

lon = ""

for row in reader:

pept = str(row["Peptide"])

pept = re.sub("\((\+\d+\.\d+)\)", "", pept)

peptides.append(pept)

return peptides

def openfasta(fast):

with open(fast, "r") as fastafile:

dic = {}

for line in fastafile:

l = line.strip()

if l[0] == ">":

cur = l

dic[l] = ""

else:

dic[cur] = dic[cur] + l

return dic

def match(text, pattern):

text = list(text.upper())

pattern = list(pattern.upper())

ans = []

cur = 0

mis = 0

i = 0

while True:

if i == len(text):

break

if text[i] != pattern[cur]:

mis += 1

if mis > 1:

mis = 0

cur = 0

continue

cur = cur + 1

i = i + 1

if cur == len(pattern):

ans.append(i - len(pattern))

cur = 0

mis = 0

continue

return ans

def job(pepts, outfile, genes):

c = 0

it = 0

towrite = []

for i in pepts:

# if it % 1000 == 0:

# with lock:

# print float(it) / float(len(pepts))

it = it + 1

found = 0

for j in genes:

m = match(genes[j], i)

if len(m) > 0:

found = 1

remb = m[0]

wh = j

c = c + len(m)

if c > 1:

found = 0

c = 0

break

if found == 1:

towrite.append("\t".join([i, str(remb), str(wh)]) + "\n")

return towrite

def worker(outfile, genes):

s = q_in.qsize()

while True:

item = q_in.get()

print "\r{0:.2f}%".format(1 - float(q_in.qsize()) / float(s))

if item is None:

break #kill thread

pepts = item

q_out.put(job(pepts, outfile, genes))

q_in.task_done()

def main(args):

num_worker_threads = int(args[4])

pept = opencsv(args[1])

l = len(pept)

howman = num_worker_threads

ll = ceil(float(l) / float(howman * 100))

remain = pept

pepties = []

while len(remain) > 0:

pepties.append(remain[0:ll])

remain = remain[ll:]

for i in pepties:

print len(i)

print l

print "Csv file loaded..."

genes = openfasta(args[2])

out = args[3]

print "Fasta file loaded..."

threads = []

with open(out, "w") as outfile:

for pepts in pepties:

q_in.put(pepts)

for i in range(num_worker_threads):

t = threading.Thread(target=worker, args=(outfile, genes, ))

# t.daemon = True

t.start()

threads.append(t)

q_in.join() # run workers

# stop workers

for _ in range(num_worker_threads):

q_in.put(None)

for t in threads:

t.join()

# print(t)

return 0

if __name__ == "__main__":

sys.exit(main(sys.argv))

在長序列中,長序列中的基因與短序列匹配是很重要的。

總結

以上是生活随笔為你收集整理的python多核运行程序怎么关闭_在多核上运行程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。