python调用time sleep,Python的time.sleep-从不唤醒

I think this is going to be one of those simple-when-you-see-it problems, but it has got me baffled.

[STOP PRESS: I was right. Solution was found. See the answers.]

I am using Python's unittest framework to test a multi-threaded app. Nice and straight forward - I have 5 or so worker threads monitoring a common queue, and a single producer thread making work-items for them. The producer thread is being triggered by a test-case.

In this test, only one task is being put on the queue. The processing it does is in the test is just a stub for the real processing, so the worker thread does a 5 second-sleep to simulate the elapsed time before the task will really be done, and the thread will be ready to get another task.

To the snippet of code is:

logging.info("Sleep starting")

time.sleep(5)

logging.info("Waking up")

Now the weird part. I see the "Sleep starting" log message, but not the Waking up message. The program locks up and doesn't respond to Keyboard Interrupt (CTRL+C). CPU load is very low.

I see the same problem in Windows and Ubuntu (Python 2.6.2).

I have pondered if an exception is occurring and being hidden, so I add "print 1/0" between the first and second line - I see the Division By Zero error being raised. I move it to after the sleep, and I never see the message.

I figured "Okay, maybe the other thread is trying to log something very very large at the same time, and it is still buffering. What is it doing?"

Well, by this time, the test has returned to the unittest, where it is pausing waiting for the thread to get going before testing the system's state.

logging.info("Test sleep starting")

time.sleep(0.25)

logging.info("Test waking up")

Wow, that looks familiar. It is freezing in exactly the same way! The first log message is appearing, the second isn't.

I have recently done a significant rewrite of the unit so I can't claim "I didn't touch anything", but I can't see anything untoward in my changes.

Suspicious areas:

I am including using Threading.Lock (because I don't know how to reason about GIL's safety, so I stick to what I know. I see nothing "deadlocky" about my code.

I am new to Python's unittest framework. Is there something it does with redirecting logging or similar that might simulate these symptoms?

No, I haven't substituted a non-standard time module!

What would prevent a thread from waking up? What else have I missed?

解决方案

Sigh.

Worker Thread #1 is sleeping, and waking up afterwards. It is then going to log the wake message, and is blocked. Only one thread can be logging at a time.

UnitTest Thread is sleeping, and waking up afterwards. It is then going to log the wake message, and is blocked. Only one thread can be logging at a time.

Worker-Thread-Not-Previously-Mentioned-In-The-Question #2 was quietly finishing the processing the PREVIOUS item in the queue, while the first Worker Thread was sleeping. It got to a log statement. One of the parameters was an object, and str() was implicitly called. The str() function on that object had a bug; it deadlocked when it accessed some of its data members. The deadlock occured while being processed by the logging function, thus keeping the logging thread-lock, and making it appear like the other threads never woke up.

The division by zero test didn't make a difference, because the result of it was an attempt to log.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中的time.sleep函数用于推迟调用线程的运行,可以通过参数secs指定秒数来表示起的时间。例如,time.sleep(5)将会使程序暂停执行5秒钟。这个函数的参数可以是任意的秒数,包括小数,可以转换为毫秒或其他单位的时间。这个函数没有返回值。下面是一个使用time.sleep函数的示例: ``` import time print("Start : %s" % time.ctime()) time.sleep(5) print("End : %s" % time.ctime()) ``` 以上代码将会输出类似以下的结果: ``` Start : Tue Feb 17 10:19:18 2013 End : Tue Feb 17 10:19:23 2013 ``` 在这个例子中,程序先打印开始时间,然后调用time.sleep(5)使程序暂停执行5秒钟,最后打印结束时间。这样可以实现在程序执行过程中暂停一段时间的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python time的休眠函数sleep方法](https://blog.csdn.net/liveshow021_jxb/article/details/113185727)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [『Python学习笔记』python time sleep()方法](https://blog.csdn.net/abc13526222160/article/details/85139204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值