python3:并行运行C文件和python脚本

时间:2018-08-27 12:47:10

标签: parallel-processing multiprocessing subprocess python-3.5

我在这篇文章中摘录了我的实际python脚本。基本上,我想并行执行一个C程序和一个Pyserial函数(C程序用于控制电动机,pySerial用于与arduino通信)。我的程序将使用Spyder3和Rasbipian在RPi3b上执行。 我已经从下面的源中弄清楚了,如果要在python中执行终端程序,则应使用subprocess类。如果要并行执行某项操作,则来自多处理的Process包将完成此工作。 因此,我将它们混合在一起,并尝试使用代码缺陷来归档我的目标。不幸没有任何成功。在调用p1进程[p1 = Process(target = run_c_file())]之后,p1进程立即开始,脚本停止,直到C文件完成。外面有人可以帮忙吗?非常感谢你!

BTW Im正在使用python 3.5 ...

我的资料来源: https://docs.python.org/3.5/library/multiprocessing.htmlhttps://docs.python.org/3.5/library/subprocess.html?highlight=subprocess

import serial_comm as ssf #My own function. Tested and working when single calling

import subprocess as sub
from multiprocessing import Process

def run_c_file():
  sub.run("./C_File") #Call the C File in the same directory. Immeidatly starts when script is at line 14 -> p1 = Process(target=run_c_file())


def run_pyserial(ser_obj):
  ssf.command(ser_obj,"Command") #Tell the arduino to do something fancy (tested and working)

ser_obj = ssf.connect()
p1 = Process(target=run_c_file())
p2 = Process(target=run_pyserial(ser_obj))

try:
    p1.start()
    p2.start()
    p1.join() #Process one should start here (as far as I understood)
    p2.join() #Process two should start here (as far as I understood)
'''The following part is still in progress'''
except KeyboardInterrupt:
    print("Aborting")

p1.terminate()
p2.terminate()

1 个答案:

答案 0 :(得分:0)

尝试

p1 = Process(target=run_c_file)
p2 = Process(target=run_pyserial, args=(ser_obj,))

当前,您正在调用函数而不是传递它。