用pySeri进行多处理的好例子

2024-05-23 14:41:32 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有什么地方可以让我看看在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()

在执行上述操作时,我面临一些问题:

  1. 流程f1根据需要执行任务。也就是说,打开LED 10秒钟,关闭LED 10秒钟,最后打开LED 10秒钟。从外观上看,尽管程序成功结束,但进程f2似乎根本没有被执行(也就是说,没有每两秒钟关闭一次LED)。这里会发生什么?

  2. 如果我使用print来打印流程中的某些内容,它不会出现在屏幕上。我很想知道提到这些例子的人是如何显示这些过程的打印输出的。


Tags: thetoledmainondefserialmultiprocessing