【发布时间】:2019-04-19 15:23:02
【问题描述】:
我正在使用线程模块在后台运行一个函数,同时执行我的脚本的其余部分。线程函数包含一个 for 循环,该循环等待外部 5 伏触发器,每 15 毫秒发生一次,然后继续下一个循环迭代。
当此代码是唯一在 PC 上运行的代码时,一切都会按预期运行。但是,当我运行其他必要的应用程序时,给 cpu 带来压力时,线程函数中的 For 循环仅在大约 90% 的时间内执行并在 15 毫秒的时间窗口内继续进行下一次迭代。
线程函数的输入是一个 ctypes 指针列表。
我在一个类中运行线程函数,所以使用多处理很棘手(我不确定这是否有帮助)。
我试图用两个类的骨架来说明下面的问题
import ctypes
import Write_transient_frames_func
import SendScriptCommands
from threading import Thread
class SlmInterface():
def __init__(self,sdk):
self.sdk = sdk
def precalculate_masks(self, mask_list):
'''takes input mask_list, a list of numpy arrays containing phase masks
outputs pointers to memory location of masks
'''
#list of pointers to locations of phase mask arrays in memory
mask_pointers = [mask.ctypes.data_as(POINTER(c_ubyte)) for mask in mask_list]
return mask_pointers
def load_precalculated_triggered(self, mask_pointers):
okay = True
print('Ready to trigger')
for arr in mask_pointers:
okay = self.Write_transient_frames_func(self.sdk, c_int(1), arr, c_bool(1), c_bool(1), c_uint(0))
assert okay, 'Failed to write frames to board'
print('completed trigger sequence')
class Experiment():
def run_experiment(self, sdk, mask_list):
slm = SlmInterface(sdk)
#list of ctypes pointers
mask_pointers = slm.precalculate_masks(mask_list)
##the threaded function
slm_thread = Thread(target=slm.load_precalculated_triggered, args = [mask_pointers])
slm_thread.start()
time.sleep(0.1)
# this function loads the 15ms trigger sequences to the hardware and begins the sequence
self.mp_output = SendScriptCommands()
是否可以加快线程函数的执行速度?并行处理会有帮助吗?还是我从根本上受限于我的 CPU?
【问题讨论】:
-
您正在使用并行处理。线程在操作系统的控制之下。我认为您对 15 毫秒响应时间的要求是不现实的。
-
谢谢,是否有替代方案可以让我在后台运行 load_precalculated_triggered 函数,然后继续执行脚本的其余部分?
-
我之前也遇到过同样的问题,发现我的线程有while循环运行并消耗cpu功率,后来我发现我必须添加'time.sleep(0.1)'作为延迟循环内的时间给另一个线程工作的时间,令人惊讶的是,我的 cpu 使用率正常,线程运行速度超快,注意:我的应用程序是一个下载管理器,它在一次下载中生成 100 多个并发线程,并且可以在同时没有速度下降
-
在试图捕获每 15 毫秒发生一次的信号的线程中休眠 100 毫秒只会使问题变得更糟。现在你每 6 次丢失约 5 个信号 - 干得好!
标签: python python-multiprocessing python-multithreading