我正努力使用下面的代码使用Multiprocessing的Process函数同时运行2个函数。我最后的努力是运行一个定时器,它将在脚本的主体内部进行检查。如果时间已经完成,主体中的依赖关系会触发自己的行为。如果时间没有完成,它会转到下一个动作。Python 3同时运行多个函数
我在MacBook Pro上与构建运行以下命令: 处理器名称:Intel酷睿2双核处理器 速度:1 核心总数:2
脚本2.66 GHz的 处理器数量:
#!/usr/bin/pyton
# MODULES
import time
from multiprocessing import Process
# GLOBAL VARIABLES
Completion = ''
# FUNCTIONS
def Timer(duration):
global Completion
Ticker = 0
while Ticker != duration:
Ticker = Ticker + 1
print(Ticker)
time.sleep(1)
Completion = '{0}TickerDone'.format(duration)
def Wait(seconds):
time.sleep(seconds)
#MAIN
P1 = Process(target = Timer(10))
P1.start()
P2 = Process(target = Wait(11))
P2.start()
P1.join()
P2.join()
print(Completion)
if Completion == '10TickerDone':
print('Good to go!\n')
else:
print('Not yet!\n')
# END
两个功能做什么是例外,然而第二功能,其旨在以等待10个+ 1 secondards用于第一功能的完成,仅开始一旦第一功能,10秒计时器,是完成。所以,在本质上,我必须等待21秒来测试10秒计时器。
结果:
$ python Test.py
1
2
3
4
5
6
7
8
9
10
10TickerDone
Good to go!
我需要做的是在并行运行这两个功能,所以如果第一个功能是执行第三功能之前完成第二功能可以测试了。
我哪里错了?
注意的文件,我尝试同样的事情线程,线程与进程替换,但有相同的结果。 – danjmwalker
嗨马克,我确实尝试写出来,因为你有它,但我得到相同的结果。 (), – danjmwalker
P1 =过程(目标=定时器(10),参数=(10,)) P2.start() P2 =过程(目标=等待,参数=(11,)) P2.start – danjmwalker