我正在开发一个项目,在这个项目中,我使用大约6个传感器输入Beaglebone Black,将这些数据连续保存到6个不同的文件中。通过另一个SO问题(https://stackoverflow.com/a/36634587/2615940),我了解到多处理模块可以为我完成这项工作,但是在运行新代码时,我只获得1个文件,而不是6个文件。如何修改此代码以获得所需的6个结果文件?在
*根据下面skrrgwasme的建议,我已经编辑了我的文件以包含Manager,但是现在代码运行,并且什么也不产生。没有错误,没有文件。只是跑步。在
代码:import Queue
import multiprocessing
import time
def emgacq(kill_queue, f_name, adcpin):
with open(f_name, '+') as f:
while True:
try:
val = kill_queue.get(block = False)
if val == STOP:
return
except Queue.Empty:
pass
an_val = ADC.read(adcpin) * 1.8
f.write("{}\t{}\n".format(ms, an_val))
def main():
#Timing stuff
start = time.time()
elapsed_seconds = time.time() - start
ms = elapsed_seconds * 1000
#Multiprcessing settings
pool = multiprocessing.Pool()
m = multiprocessing.Manager()
kill_queue = m.Queue()
#All the arguments we need run thru emgacq()
arg_list = [
(kill_queue, 'HamLeft', 'AIN1'),
(kill_queue, 'HamRight', 'AIN2'),
(kill_queue, 'QuadLeft', 'AIN3'),
(kill_queue, 'QuadRight', 'AIN4'),
(kill_queue, 'GastLeft', 'AIN5'),
(kill_queue, 'GastRight', 'AIN6'),
]
for a in arg_list:
pool.apply_async(emgacq, args=a)
try:
while True:
time.sleep(60)
except keyboardInterrupt:
for a in arg_list:
kill_queue.put(STOP)
pool.close()
pool.join()
raise f.close()
if __name__ == "__main__":
main()