我如何将这个东西从多线程转换为多处理?使用多线程时,实际上运行速度较慢,而使用的CPU却不多。因此,我希望多处理可能有所帮助。
def multiprocess(sentences):
responselist = []
#called by each thread
def processfunction(asentence,i):
pro_sentence = processthesentence(asentence[0],asentence[1],asentence[2],asentence[3],asentence[4],asentence[5],asentence[6],asentence[7],asentence[8])
mytyple = asentence,pro_sentence
responselist.append(mytyple)
# ----- function end --------- #
#start threading1
threadlist = []
for i in range (2):
asentence = sentences[i]
t = Thread(target=processfunction, args=(asentence,i,))
threadlist.append(t)
t.start()
for thr in threadlist:
thr.join()
return responselist
我尝试了一下(用一个单词替换“ Thread-Process”,但这是行不通的):
from multiprocessing import Process
def processthesentence(asentence):
return asentence + " done"
def multiprocess(sentences):
responselist = []
#called by each thread
def processfunction(asentence,i):
pro_sentence = processthesentence(asentence)
mytyple = asentence,pro_sentence
responselist.append(mytyple)
# ----- function end --------- #
#start threading1
threadlist = []
for i in range (2):
asentence = sentences[i]
t = Process(target=processfunction, args=(asentence,i,))
threadlist.append(t)
t.start()
for thr in threadlist:
thr.join()
return responselist
sentences = []
sentences.append("I like apples.")
sentences.append("Green apples are bad.")
multiprocess(sentences)
尝试了greenevent,但出现了一些错误:
import greenlet
import gevent
def dotheprocess(sentences):
responselist = []
#called by each thread
def task(asentence):
thesentence = processsentence(asentence[0],asentence[1],asentence[2],asentence[3],asentence[4],asentence[5],asentence[6],asentence[7],asentence[8])
mytyple = asentence,thesentence
responselist.append(mytyple)
# ----- function end --------- #
def asynchronous():
threads = [gevent.spawn(task, asentence) for asentence in sentences]
gevent.joinall(threads)
asynchronous()
return responselist
这是最适合我的方法-不使用它的速度快约50%:
def processthesentence(asentence):
return asentence
import multiprocessing as mympro
if __name__=="__main__":
sentences = ['I like something','Great cool']
numberofprocesses = 3
thepool = mympro.Pool(processes=numberofprocesses)
results = [thepool.apply_async(processthesentence, args=(asent,)) for asent in sentences]
output = [item.get() for item in results]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句