【发布时间】:2018-08-27 12:47:10
【问题描述】:
我已经截取了我在这篇文章中编写的实际 python 脚本。基本上我想要一个 C 程序和一个 Pyserial 函数并行执行(C 程序用于控制电机,pySerial 用于与 arduino 通信)。我的程序将使用 Spyder3 和 Rasbipian 在 RPi3b 上执行。 我已经从下面的资料中了解到,如果你想在 python 中执行一个终端程序,你应该使用 subprocess 类。如果您想并行执行某些事情,来自多处理的 Process 包将完成这项工作。 因此,我将它们混合在一起,并尝试使用以下代码来归档我的目标。不幸的是没有任何成功。 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()
【问题讨论】:
-
如果你想让你的 Python 程序启动一个外部程序,那么这个外部程序的实现语言与它几乎没有关系。
标签: parallel-processing multiprocessing subprocess python-3.5