同时运行两个进程 [英] running two process simultaneously

查看:116
本文介绍了同时运行两个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图同时运行2个进程,但只有第一个运行

I'm trying to run 2 processes simultaneously, but only the first one runs

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()

推荐答案

以下内容肯定可以正常运行,但请尝试将其作为模块运行.请勿尝试在控制台或Jupiter笔记本中使用,因为笔记本永远不会满足以下条件:如果名称 =='主要'". 将整个代码保存在一个名为process.py的文件中,然后在命令提示符下运行它. 编辑 - 一切正常.刚才我试过-

Below will work for sure but try to run this as a module.Don't try in console or Jupiter notebook as notebook will never satisfy the condition "if name == 'main'". Save the entire code in a file say process.py and run it from command prompt. Edit - It's working fine. Just now I tried -

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()

这篇关于同时运行两个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆