python多线程代码报错,个人觉得是没写参数,但不会改,请给出参数的修改代码
def handleCalc16(self):
if self.timer3 is None:
self.timer3 = QTimer()
self.timer3.start(0.1)
self.timer3.timeout.connect(self.handleCalc16)
pp = pprint.PrettyPrinter(indent=4)
with artdaq.Task() as task:
task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
task.timing.cfg_samp_clk_timing(
1000, sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=2)
data = task.read(number_of_samples_per_channel=1)
data = ','.join(str(i) for i in data)
data = float(data) * 40
self.ui.textBrowser_6.append('%.2f' % ((float('%.5f' % data)) ))
self.ui.textBrowser_6.moveCursor(self.ui.textBrowser_6.textCursor().End)
thread = Thread(target=handleCalc16)
thread.start()
以下是报错信息
File "D:\PyCharm 2019.1.1\python3.7.9\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: handleCalc16() missing 1 required positional argument: 'self'
使用Thread创建线程时,target参数传入线程函数,线程函数的参数则需要args参数传入。根据题主的意图,thread=Thread(target=handleCalc16)一句,似应改为thread=Thread(target=handleCalc16, args=(x,))。不过,从题主的代码看,handleCalc16应该是一个类的方法,其参数self是类实例,是隐含的,因此x可能是虚拟的。正确的写法是,先实例化类,然后用实例的handleCalc16方法作为线程函数。比如,假定类名为SomeClass,实例a=SomeClass(),然后thread=Thread(target=a.handleCalc16),如此这般,就应该没有问题了。