python while sleep_Python while循环内的准确睡眠/延迟

该博客探讨了如何在Python中实现精确的循环频率控制,以调用外部函数。作者面临的问题是使用`time.sleep()`的精度不足,特别是在多线程环境中。提出了一种解决方案,即通过比较当前时间和起始时间加周期来控制循环,但在实践中导致函数仅执行一次。建议使用较小的`time.sleep()`精度进行调整,并考虑增加线程切换频率以提高准确性。此外,还讨论了是否应该在这样的场景中使用线程。
摘要由CSDN通过智能技术生成

I have a while True loop which sends variables to an external function, and then uses the returned values. This send/receive process has a user-configurable frequency, which is saved and read from an external .ini configuration file.

I've tried time.sleep(1 / Frequency), but am not satisfied with the accuracy, given the number of threads being used elsewhere. E.g. a frequency of 60Hz (period of 0.0166667) is giving an 'actual' time.sleep() period of ~0.0311.

My preference would be to use an additional while loop, which compares the current time to the start time plus the period, as follows:

EndTime = time.time() + (1 / Frequency)

while time.time() - EndTime < 0:

sleep(0)

This would fit into the end of my while True function as follows:

while True:

A = random.randint(0, 5)

B = random.randint(0, 10)

C = random.randint(0, 20)

Values = ExternalFunction.main(Variable_A = A, Variable_B = B, Variable_C = C)

Return_A = Values['A_Out']

Return_B = Values['B_Out']

Return_C = Values['C_Out']

#Updated other functions with Return_A, Return_B and Return_C

EndTime = time.time() + (1 / Frequency)

while time.time() - EndTime < 0:

time.sleep(0)

I'm missing something, as the addition of the while loop causes the function to execute once only. How can I get the above to function correctly? Is this the best approach to 'accurate' frequency control on a non-real time operating system? Should I be using threading for this particular component? I'm testing this function on both Windows 7 (64-bit) and Ubuntu (64-bit).

解决方案

If I understood your question correctly, you want to execute ExternalFunction.main at a given frequency. The problem is that the execution of ExternalFunction.main itself takes some time. If you don't need very fine precision -- it seems that you don't -- my suggestion is doing something like this.

import time

frequency = 1 # Hz

period = 1.0/frequency

while True:

time_before = time.time()

[...]

ExternalFunction.main([...])

[...]

while (time.time() - time_before) < period:

time.sleep(0.001) # precision here

You may tune the precision to your needs. Greater precision (smaller number) will make the inner while loop execute more often.

This achieves decent results when not using threads. However, when using Python threads, the GIL (Global Interpreter Lock) makes sure only one thread runs at a time. If you have a huge number of threads it may be that it is taking way too much time for the program to go back to your main thread. Increasing the frequency Python changes between threads may give you more accurate delays.

Add this to the beginning of your code to increase the thread switching frequency.

import sys

sys.setcheckinterval(1)

1 is the number of instructions executed on each thread before switching (the default is 100), a larger number improves performance but will increase the threading switching time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值