有没有什么地方可以查看在Python的多处理环境中执行pySerial操作的示例?
===更新上述问题===
Arduino代码:
//Initialize the pins
void setup()
{
//Start serial communication
}
void loop()
{
//Keep polling to see if any input is present at the serial PORT
//If present perform the action specified.
//In my case : TURN ON the led or TURN OFF.
}
类似于Python前端的代码:
作为基本参考,我使用了 Painless Concurrency: The multiprocessing Module ,(PDF,3.0 MB)。
#import the different modules like time,multiprocessing
#Define the two parallel processes:
def f1(sequence):
#open the serial port and perform the task of controlling the led's
#As mentioned in the answer to the above question : Turn ON or OFF as required
#The 10 seconds ON, then the 10 seconds OFF,finally the 10 seconds ON.
def f2(sequence):
#Perform the task of turning the LED's off every 2 seconds as mentioned.
#To do this copy the present conditions of the led.
#Turn off the led.
#Turn it back to the saved condition.
def main():
print "Starting main program"
hilo1 = multiprocessing.Process(target=f1, args=(sequence))
hilo2 = multiprocessing.Process(target=f2, args=(sequence))
print "Launching threads"
hilo1.start()
hilo2.start()
hilo1.join()
hilo2.join()
print "Done"
if ____name____ == '____main____':
main()
我在执行上述操作时遇到了一些问题:
进程f1根据需要执行任务。那就是打开LED 10秒钟,关闭LED 10秒钟,最后打开LED 10秒钟。从外观上看,虽然程序成功结束,但看起来过程f2没有被执行(即,每两秒没有关闭LED)。这可能会发生什么?
如果我使用print在流程中打印某些内容,则它不会出现在屏幕上。我很想知道提到这些例子的人如何能够显示这些过程的打印输出。
答案 0 :(得分:0)
你为什么用加号? join阻塞调用线程,直到调用join()方法的进程终止或直到发生可选超时。 这就是为什么你的f2没有启动因为f1正在运行。
在main中尝试此代码
procs = []
procs.append(Process(target=f1, args=(sequence))
procs.append(Process(target=f2, args=(sequence))
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)
答案 1 :(得分:-1)
好吧,here你在GUI应用程序(PyQt)中有一个PySerial监控的代码示例,在一个单独的线程中运行。