开发者

Good examples for multiprocessing with pySerial

Is there any place where I can have a look at examples which perform pySerial operations in a multiprocessing environment in Python?

===Update to the above question ===

Code for 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.
}

Similarly code for the Python front-end:

For basic reference I used 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()

There are a few issues I am facing on performing the above:

  1. Process f1 performs the task as required. That is to turn on the LED for 10 seconds, turn off the LED for 10 seconds, and finally turn on the LED for 10 seconds. By the look of it, it looks like the process f2 does not get executed (that is, there is no turning OFF of the LED every two seconds) at all though the program ends successfully. What could be开发者_JS百科 happening here?

  2. If I use print to print something in the processes, it does not appear on the screen. I am curious to know as to how the people mentioning the examples were able to display the output of the prints of the processes.


why have you used join ? join blocks the calling thread until the process whose join() method is called terminates or until the optional timeout occurs. Thats why your f2 isn't starting because f1 is running.

try this code in main instead

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)


Well, here you have a code sample of PySerial monitoring in a GUI app (PyQt), running in a separate thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜