您好我不确定我是否理解多处理模块并希望寻求建议。我正在审查此处http://revista.python.org.ar/1/html-en/PET-1-processing.pdf中的无痛并发示例。以下是我正在测试的代码。
import multiprocessing
import time
x = 4
y = 6
def f1(x,y):
x = x+y
print 'F1:', x
def f2(x,y):
y = x-y
print 'F2:', y
def main():
print "Starting main program"
hilo1 = multiprocessing.Process(target=f1, args=(x,y))
hilo2 = multiprocessing.Process(target=f2, args=(x,y))
print "Launching threads"
hilo1.start()
hilo2.start()
hilo1.join()
hilo2.join()
print "Both threads finished"
print "Ending program"
print "X:", x, "Y:", y
if __name__=='__main__':
main()
输出结果为:
Starting main program
Launching threads
Both threads finished
Ending program
X: 4 Y: 6
我期待更接近的事情:
Starting main program
Launching threads
F1: 10
F2: -2
Both threads finished
Ending program
X: 4 Y: 6
这是我将main()更改为:
时的输出def main():
print "Starting main program"
hilo1 = f1(x,y)
hilo2 = f2(x,y)
print "Launching threads"
print "Both threads finished"
print "Ending program"
print "X:", x, "Y:", y
多处理线程是否正在运行?检查每个函数是否使用多处理运行的最佳方法是什么?