开发者

Python Threading with Timer

开发者 https://www.devze.com 2023-01-08 10:33 出处:网络
I would like 3 Threads in Python to run for n seconds. I want to start them all at the same time and have them f开发者_运维知识库inish at the same time (within milliseconds). How do I do this?

I would like 3 Threads in Python to run for n seconds. I want to start them all at the same time and have them f开发者_运维知识库inish at the same time (within milliseconds). How do I do this?

threading.Timer only starts after the previous one has been completed.


import threading 
import time

class A(threading.Thread):
  def run(self):
     print "here", time.time()
     time.sleep(10)
     print "there", time.time()


if __name__=="__main__":
  for i in range(3):
     a = A()
     a.start()

prints:

here 1279553593.49
here 1279553593.49
here 1279553593.49
there 1279553603.5
there 1279553603.5
there 1279553603.5
0

精彩评论

暂无评论...
验证码 换一张
取 消