aja*_*aja 2 python multiprocessing
我正在尝试同时运行 2 个进程,但只有第一个运行
def add():
while True:
print (1)
time.sleep(3)
def sud():
while True:
print(0)
time.sleep(3)
p1 = multiprocessing.Process(target=add)
p1.run()
p = multiprocessing.Process(target=sud)
p.run()
Run Code Online (Sandbox Code Playgroud)
下面肯定会工作,但尝试将其作为模块运行。不要在控制台或 Jupiter notebook 中尝试,因为 notebook 永远不会满足条件“if name == ' main '”。将整个代码保存在一个文件中,比如 process.py 并从命令提示符运行它。编辑 - 它工作正常。刚才我试过——
import multiprocessing
import time
def add():
while True:
print (1)
time.sleep(3)
def sud():
while True:
print(0)
time.sleep(3)
if __name__ == '__main__':
p1 = multiprocessing.Process(name='p1', target=add)
p = multiprocessing.Process(name='p', target=sud)
p1.start()
p.start()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14954 次 |
| 最近记录: |