在Python中将多线程转换为多处理



如何将这个东西从多线程转换为多处理?使用多线程时,它实际上运行得更慢,而没有使用太多的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

我试过这个(用进程替换一个词-线程,但这不起作用):

  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

尝试使用gevent来生成多个greenlet,这将允许您使用其他CPU。这是你的一个例子。确保Queue能够在gevent

的上下文切换中正常工作。<>之前导入一种绿色小鸟进口gevent从event导入monkeymonkey.patch_all ()def dotheprocess(句子):queue = gevent.queue.Queue()由每个线程调用def任务(asentence):句子= processsentence(句子[0],句子[1],句子[2],句子[3],句子[4],句子[5],句子[6],句子[7],句子[8])queue.put ((asentence thesentence))线程= [gevent.]为句子中的句子生成(任务,句子)]gevent.joinall(线程)返回队列#用句子调用dotheprocess函数

线程不会使函数更快,除非在线程中有一些等待函数(I/O实现)。多处理在理论上会有帮助,但是简单的函数不会从中受益太多,因为开销很大,所以要小心使用它。使用Manager作为共享变量

from multiprocessing import Process, Manager, freeze_support
class multiProcess():
    def __init__(self, sentences):
        self.responseList = Manager().list()
        self.processList = []
        self.sentences = sentences
    def processSentence(self,a0,a1,a2,a3,a4,a5,a6,a7,a8):
        reversedValue = a8+a7+a6+a5+a4+a3+a2+a1+a0
        return reversedValue
    #called by each process
    def processFunction(self,asentence):
        pro_sentence = self.processSentence(asentence[0],asentence[1],asentence[2],asentence[3],asentence[4],asentence[5],asentence[6],asentence[7],asentence[8])
        mytuple = (asentence,pro_sentence)
        self.responseList.append(mytuple)
        return
    def run(self):
        for i in range(2):
            asentence = self.sentences[i]
            p = Process(target=self.processFunction, args=(asentence,))
            self.processList.append(p)
            p.start()
        for pro in self.processList:
            pro.join()
        return self.responseList

if __name__=="__main__":
    freeze_support()
    sentences = ['interesting','wonderful']
    output = multiProcess(sentences).run()
    print(output)

这对我来说是最有效的-它比不使用它快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]

相关内容

  • 没有找到相关文章

最新更新