Python time.sleep()阻止线程超过预期

时间:2017-10-20 13:57:39

标签: python multithreading python-2.7 flask thread-sleep

我有一个python(Flask)应用程序运行一个应该每秒打印一次的线程,但似乎没有按预期工作。

奇怪的行为(使用time.sleep):

def pending():
  while True:
    print "time: ", datetime.datetime.now(), threading.current_thread()
    time.sleep(1)

thread = Thread(target = pending, args = ())
thread.start()

输出(一次打印所有内容,查看左侧的输出时间)。每张照片都在13:53:11打印,而不是每秒打印

2017-10-20T13:53:11.569382+00:00 app[web.1]: time:  2017-10-20 13:52:45.544440 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569383+00:00 app[web.1]: time:  2017-10-20 13:52:46.544843 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569384+00:00 app[web.1]: time:  2017-10-20 13:52:47.545041 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569384+00:00 app[web.1]: time:  2017-10-20 13:52:48.545815 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569385+00:00 app[web.1]: time:  2017-10-20 13:52:49.547043 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569385+00:00 app[web.1]: time:  2017-10-20 13:52:50.547821 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569386+00:00 app[web.1]: time:  2017-10-20 13:52:51.549031 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569387+00:00 app[web.1]: time:  2017-10-20 13:52:52.550236 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569387+00:00 app[web.1]: time:  2017-10-20 13:52:53.550584 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569388+00:00 app[web.1]: time:  2017-10-20 13:52:54.551613 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569389+00:00 app[web.1]: time:  2017-10-20 13:52:55.552859 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569389+00:00 app[web.1]: time:  2017-10-20 13:52:56.554084 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569390+00:00 app[web.1]: time:  2017-10-20 13:52:57.555101 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569390+00:00 app[web.1]: time:  2017-10-20 13:52:58.556311 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569391+00:00 app[web.1]: time:  2017-10-20 13:52:59.557522 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569392+00:00 app[web.1]: time:  2017-10-20 13:53:00.558695 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569392+00:00 app[web.1]: time:  2017-10-20 13:53:01.559222 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569393+00:00 app[web.1]: time:  2017-10-20 13:53:02.560393 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569394+00:00 app[web.1]: time:  2017-10-20 13:53:03.561595 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569394+00:00 app[web.1]: time:  2017-10-20 13:53:04.562276 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569395+00:00 app[web.1]: time:  2017-10-20 13:53:05.563509 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569396+00:00 app[web.1]: time:  2017-10-20 13:53:06.564771 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569397+00:00 app[web.1]: time:  2017-10-20 13:53:07.566009 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569398+00:00 app[web.1]: time:  2017-10-20 13:53:08.566247 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569398+00:00 app[web.1]: time:  2017-10-20 13:53:09.566850 <Thread(Thread-1, started 140522956039936)>
2017-10-20T13:53:11.569399+00:00 app[web.1]: time:  2017-10-20 13:53:10.568075 <Thread(Thread-1, started 140522956039936)>

正常行为(没有time.sleep):

def pending():
  while True:
    print "time: ", datetime.datetime.now(), threading.current_thread()

thread = Thread(target = pending, args = ())
thread.start()

输出(按预期运行,始终打印):

2017-10-20T13:41:00.999954+00:00 app[web.1]: time:  2017-10-20 13:41:00.999677 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999954+00:00 app[web.1]: time:  2017-10-20 13:41:00.999694 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999955+00:00 app[web.1]: time:  2017-10-20 13:41:00.999711 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999955+00:00 app[web.1]: time:  2017-10-20 13:41:00.999728 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999956+00:00 app[web.1]: time:  2017-10-20 13:41:00.999745 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999956+00:00 app[web.1]: time:  2017-10-20 13:41:00.999762 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999956+00:00 app[web.1]: time:  2017-10-20 13:41:00.999780 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999957+00:00 app[web.1]: time:  2017-10-20 13:41:00.999797 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999958+00:00 app[web.1]: time:  2017-10-20 13:41:00.999814 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999958+00:00 app[web.1]: time:  2017-10-20 13:41:00.999831 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999959+00:00 app[web.1]: time:  2017-10-20 13:41:00.999849 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999959+00:00 app[web.1]: time:  2017-10-20 13:41:00.999866 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:00.999962+00:00 app[web.1]: time:  2017-10-20 13:41:00.999883 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000981+00:00 app[web.1]: time:  2017-10-20 13:41:00.999901 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000983+00:00 app[web.1]: time:  2017-10-20 13:41:00.999938 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000983+00:00 app[web.1]: time:  2017-10-20 13:41:00.999975 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000984+00:00 app[web.1]: time:  2017-10-20 13:41:00.999999 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000984+00:00 app[web.1]: time:  2017-10-20 13:41:01.000017 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000985+00:00 app[web.1]: time:  2017-10-20 13:41:01.000035 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000985+00:00 app[web.1]: time:  2017-10-20 13:41:01.000053 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000986+00:00 app[web.1]: time:  2017-10-20 13:41:01.000070 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000986+00:00 app[web.1]: time:  2017-10-20 13:41:01.000087 <Thread(Thread-1, started 140140468745984)>
2017-10-20T13:41:01.000987+00:00 app[web.1]: time:  2017-10-20 13:41:01.000105 <Thread(Thread-1, started 140140468745984)>

我也尝试使用while not exit_flag.wait(timeout=1):代替while True:,但行为是相同的

时间导入为import time

0 个答案:

没有答案