0

有什么地方可以让我查看在 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 打印某些内容,它不会出现在屏幕上。我很想知道提到这些示例的人如何能够显示流程打印的输出。

4

2 回答 2

0

你为什么用 join ?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)
于 2014-01-02T16:07:53.577 回答
-1

好吧,这里有一个 GUI 应用程序 (PyQt) 中的 PySerial 监控代码示例,它在单独的线程中运行。

于 2011-09-07T05:02:26.490 回答