如何将这个东西从多线程转换为多处理?使用多线程它实际上运行较慢,而使用的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 with 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)
尝试绿色但有一些错误:
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
答案 0 :(得分:1)
尝试使用gevent生成多个greenlet,以允许您使用其他CPU。这是一个根据你的例子。看到队列用于能够在gevent的上下文切换中正常工作
import greenlet import gevent from gevent import monkey monkey.patch_all() def dotheprocess(sentences): queue = gevent.queue.Queue() #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]) queue.put((asentence,thesentence)) threads = [gevent.spawn(task, asentence) for asentence in sentences] gevent.joinall(threads) return queue #call the dotheprocess function with your sentences
答案 1 :(得分:1)
除非你的线程中有一些等待函数(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)
答案 2 :(得分:0)
这对我来说是最好的 - 它在不使用它的情况下快了大约50%:
function realizaProceso(valorCaja1, valorCaja2){
var parametros = {
"valorCaja1" : valorCaja1,
"valorCaja2" : valorCaja2
};
$.ajax({
data: parametros,
url: 'ejemplo_ajax_proceso.php',
type: 'post',
beforeSend: function () {
$("#resultado").html("Procesando, espere por favor...");
},
success: function (response) {
$("#resultado").html(response);
}
});