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

python3: Run C file and python script in parallel

我得到了我用这个 post 编写的实际 python 脚本的片段。基本上我想并行执行一个 C 程序和一个 Pyserial 函数(C 程序用于控制电机,pySerial 用于与 arduino 通信)。我的程序将使用 Spyder3 和 Rasbipian 在 RPi3b 上执行。 我已经从下面的资源中了解到,如果你想在 python 中执行终端程序,你应该使用子进程 class。如果你想并行执行一些事情,来自 multiprocessing 的 Process 包将完成这项工作。 所以我将它们混合在一起并尝试通过使用代码 bleow 来归档我的目标。不幸的是没有任何成功。 p1 进程在调用 p1 进程后立即启动 [ p1 = Process(target=run_c_file()) ] 并且脚本停止,直到 C 文件完成。有没有人可以帮忙?非常感谢!

顺便说一句,我正在使用 python 3.5...

我的消息来源: https://docs.python.org/3.5/library/multiprocessing.html , https://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()

尝试

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

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