datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

Threading. Run two tasks independently

Sat Jan 26, 2013 2:21 am

I'm very new to python and pi.

I can use this bash script to run two tasks simultaneously independent of each other

Code: Select all

#!/bin/bash

/home/pi/gpio/led_Startup.py &

# Blink led 1 at random
/home/pi/gpio/blink_Led_1.py &

# Blink led 2 at random
/home/pi/relay/blink_Led_2.py &
There has to be a better way within a pythons script but what do I look for and where?

Is it threading, multitasking, parallel processing?

souperPi
Posts: 1
Joined: Sat Jan 26, 2013 4:40 am

Re: Threading. Run two tasks independently

Sat Jan 26, 2013 4:48 am

Datanaut,

Check out the "threading" package in Python, that should do what you're looking for. Your example doesn't need it, but the queue package is very straightforward for communicating between threads.

The "multiprocessing" package could be used too, but for your example I think that would be overkill.

datanaut
Posts: 11
Joined: Mon Jan 21, 2013 10:05 pm
Location: London W9

Re: Threading. Run two tasks independently

Sun Jan 27, 2013 3:13 am

souperPi wrote:Datanaut,

Check out the "threading" package in Python, that should do what you're looking for. Your example doesn't need it, but the queue package is very straightforward for communicating between threads.

The "multiprocessing" package could be used too, but for your example I think that would be overkill.
Thanks. I found this now I know what to look for and hacked it to run 5 'while loops' simultaneously

Code: Select all

import time
from threading import Thread

def myfunc(i):
    print "sleeping 5 sec from thread %d" % i
    time.sleep(5)
    print "finished sleeping from thread %d" % i

for i in range(10):
    t = Thread(target=myfunc, args=(i,))
    t.start()
Anyone know how to set a thread to SOLVED?

Return to “Python”