4、简单多进程程序

1、两个进程同时运行,由于程序运行时间间隔比较短,所以等于同时运行

分为主进程和子进程,Process()为子进

 1 from time import sleep
 2 # 导入时间函数
 3 from multiprocessing  import  Process
 4 # 导入进程函数
 5 import os
 6 def run(str):
 7     while True:
 8         print("h3h is a %s name --%s"%(str, os.getpid()))
 9         # os.getpid() 获取进程ID
10         sleep(1.1)
11 
12 if __name__ == "__main__":
13     print("这是主进程--%s"%(os.getpid()))
14     p = Process(target=run, args=("good",))
15     # args 传递的是数组,使用传递一个元素时,后面需要加“,”
16     p.start()
17     # 导入进程,并运行
18     while True:
19         print("h3h is a nice name")
20         sleep(1)

猜你喜欢

转载自www.cnblogs.com/huihenghuang/p/9094164.html