|
|

楼主 |
发表于 2019-7-8 15:42:19
|
显示全部楼层
这里修正一个地方,子进程是执行了的,但是却无法执行print()函数,例如在原来的代码中加入
with open("123.txt","w",encoding="UTF-8")as f:#在子进程中加入一个创建123.txt文本,其内容为abc。
f.write("abc")
现在的代码如下:
from multiprocessing import Process
from time import sleep
import os
def run(str):
with open("123.txt","w",encoding="UTF-8")as f:#在子进程中加入一个创建123.txt文本,其内容为abc。
f.write("abc")
while True:
#os.getpid()获取当前进程id号
#os.getppid()获取当前进程的父进程的id号
print("sunck is a %s man--%s--%s"%(str,os.getpid(),os.getppid()))
sleep(1.2)
if __name__ == "__main__":
print("主(父)进程启动--%s"%(os.getpid()))
#创建子进程
#target说明进程执行的任务
p = Process(target=run,args=("nice",))
#启动进程
p.start()
while True:
print("sunck is a good man")
sleep(1)
执行现在的代码后可以看到,的确生成了对应的123.txt文档,证明子进程是执行了的,但是为什么却无法执行print()函数呢?求大婶们指点迷津 |
|