Python 创建函数并将其作为单进程的完整代码

前端之家收集整理的这篇文章主要介绍了Python 创建函数并将其作为单进程的完整代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
感兴趣Python 创建函数并将其作为单进程的完整代码的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。<br>
  1. # 编程之家 (jb51.cc)
  2. import multiprocessing
  3. import time
  4. def worker(interval):
  5. n = 5
  6. while n > 0:
  7. print("The time is {0}".format(time.ctime()))
  8. time.sleep(interval)
  9. n -= 1
  10. if __name__ == "__main__":
  11. p = multiprocessing.Process(target = worker,args = (3,))
  12. p.start()
  13. print "p.pid:",p.pid
  14. print "p.name:",p.name
  15. print "p.is_alive:",p.is_alive()
结果:
p.pid: 8736
p.name: Process-1
p.is_alive: True
The time is Tue Apr 21 20:55:12 2015
The time is Tue Apr 21 20:55:15 2015
The time is Tue Apr 21 20:55:18 2015
The time is Tue Apr 21 20:55:21 2015
The time is Tue Apr 21 20:55:24 2015

猜你在找的Python相关文章