鱼C论坛

 找回密码
 立即注册
查看: 1772|回复: 5

[已解决]python如何同时运行两个进程?

[复制链接]
发表于 2018-2-22 08:46:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
比如现在有一个程序,程序内分为A 和B两个功能。现在想要在0~20sec 时间内运行功能A, 在15~30sec 运行功能B, 这里面有 5 sec A和B是同时运行的。
以前没有接触过并行的这类编程,请问在python中要怎么实现呢
如果可以的话,计时这块也请帮忙分析一下,谢谢!
最佳答案
2018-2-22 15:21:00
调用多线程咯
  1. def A():
  2.     print("I am function A, Start!")
  3.     import time
  4.     time.sleep(30)
  5.     print("I am function A, Finished!")
  6.    
  7. def B():
  8.     print("I am function B, Start!")
  9.     import time
  10.     time.sleep(30)
  11.     print("I am function B, Finished!")
  12.    
  13. import threading   
  14. tdA = threading.Thread(target=A)
  15. tdB = threading.Thread(target=B)

  16. def main():
  17.     import time
  18.     st = time.time()
  19.     deltatime = -1
  20.     while deltatime < 60:
  21.         if 0<deltatime<20:
  22.             if not tdA.isAlive():
  23.                 tdA.start()
  24.         if 15<deltatime<30:
  25.             if not tdB.isAlive():
  26.                 tdB.start()
  27.         deltatime = time.time()-st
  28.         print(int(deltatime),"second")
  29.         time.sleep(1)

  30. main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-22 12:09:28 | 显示全部楼层
time 模块咯。这是延时 不是两个进程
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 12:43:23 | 显示全部楼层
Python官方文档就有关于并行的
->中文版点击这里<-
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 15:21:00 | 显示全部楼层    本楼为最佳答案   
调用多线程咯
  1. def A():
  2.     print("I am function A, Start!")
  3.     import time
  4.     time.sleep(30)
  5.     print("I am function A, Finished!")
  6.    
  7. def B():
  8.     print("I am function B, Start!")
  9.     import time
  10.     time.sleep(30)
  11.     print("I am function B, Finished!")
  12.    
  13. import threading   
  14. tdA = threading.Thread(target=A)
  15. tdB = threading.Thread(target=B)

  16. def main():
  17.     import time
  18.     st = time.time()
  19.     deltatime = -1
  20.     while deltatime < 60:
  21.         if 0<deltatime<20:
  22.             if not tdA.isAlive():
  23.                 tdA.start()
  24.         if 15<deltatime<30:
  25.             if not tdB.isAlive():
  26.                 tdB.start()
  27.         deltatime = time.time()-st
  28.         print(int(deltatime),"second")
  29.         time.sleep(1)

  30. main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 15:53:55 | 显示全部楼层
2个进程开始, 其中第二个进程在第一个进程开始后等2秒再开始.
时间缩短为了节省时间,

  1. from time import ctime, sleep
  2. import multiprocessing

  3. def a(msg):
  4.     print('i am processing %s,started at '%msg, ctime())
  5.     sleep(4)
  6.     print('i am processing %s,ended at '%msg, ctime())

  7. if __name__=="__main__":
  8.     p1=multiprocessing.Process(target=a, args=('1'))
  9.     p1.start()
  10.     sleep(2)
  11.     p2=multiprocessing.Process(target=a, args=('2'))
  12.     p2.start()
  13.     print('main end at '+ctime())
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-22 16:01:28 | 显示全部楼层
使用进程池来管理进程, 可以更好的利用多核CPU的性能,

同时控制运行进程数是4个进程,
如下

  1. from time import ctime, sleep
  2. import multiprocessing

  3. def a(msg):
  4.     print('i am processing %s,started at '%msg, ctime())
  5.     sleep(4)
  6.     print('i am processing %s,ended at '%msg, ctime())

  7. if __name__=="__main__":
  8.     pool=multiprocessing.Pool(processes=4)
  9.     for i in range(5):
  10.         pool.apply_async(a, (str(i), ))
  11.     pool.close()
  12.     pool.join()
  13.     print('main end at '+ctime())
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 17:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表