代码拉取完成,页面将自动刷新
同步操作将从 pish7/stdpython 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import time
import multiprocessing
#一个可调用对象可用作子进程运行
def clock(interval):
while True:
print("P1: The time is {}".format(time.ctime()))
time.sleep(interval)
#通过继承 multiprocessing.Process 也可以定义一个子进程
class ClockProcess(multiprocessing.Process):
def __init__(self, interval):
multiprocessing.Process.__init__(self)
self.interval = interval
#重新定义 run 函数(进程启动时运行的函数)
def run(self):
while True:
print("P2: The time is {}".format(time.ctime()))
time.sleep(self.interval)
# 打开程序第一个参数是一个列表(程序名称,参数)
# 第二个参数是缓冲区大小
# stdin,stdout是设置是否打开这些管道,如果他的值是subprocess.PIPE的话,
# 就会打开,同stdin一样的还有stderr
# child = subprocess.Popen(["python","in.py"], bufsize=1024, stdin=subprocess.PIPE,
# stdout=subprocess.PIPE)
# i = 0
# while True:
# if i > 10:
# break
# else:
# i += 1
# child.stdin.write(str(i).encode())
# child.stdin.write(b"\n")
# child.stdin.write(str(i+1).encode())
# child.stdin.write(b"\n")
# child.stdin.flush()
# fb = child.stdout.readline()
# print(fb.decode().strip())
# sleep(1)
# child.kill()
if __name__ == "__main__":
#创建一个子进程
p1 = multiprocessing.Process(target=clock, args=(5,))
p2 = ClockProcess(5)
#后台进程
p1.daemon = True
p2.daemon = True
#启动子进程
p1.start()
p2.start()
#因为两个子进程是后台进程,所以主进程不能退出,否则这些子进程也将退出
input()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。