0

I have two threads, and, I want one thread to run for 10 seconds, and then have this thread stop, whilst another thread executes and then the first thread starts up again; this process is repeated. So e.g.

from threading import Thread 
import sys  
import time

class Worker(Thread):

    Listened = False; 

    def __init__(self):

        while 1:
           if(self.Listened == False):
              time.sleep(0)
           else:
            time.sleep(20)

        for x in range(0, 10):
            print "I'm working"
            self.Listened = True

    class Processor(Thread):
Listened = False;

def __init__(self):
    # this is where I'm confused!!

  Worker().start()
  Processer().start()

(P.S. I have indented correctly, however, posting seems to have messed it up a bit)

Basically, what I want is:

The worker thread works for 10 seconds (or so) and then stops, the "processor" starts up and, once the processor has processed the data from the last run of the "Worker" thread, it then re-starts the "worker" thread up. I don't specifically have to re-start the "worker" thread from that current position, it can start from the beginning.

Does anyone have any ideas?

Edited by phorce

2
Contributors
1
Reply
59
Views
3 Years
Discussion Span
Last Post by Gribouillis
1

Here is an example with 2 worker threads. It uses Condition objects to synchronize threads. It is relatively easy to understand if you remember that only one thread may own a given condition at a given time, which means for example that a worker blocks on with G.wcond if another thread is in a with G.wcond section, unless the other thread is running a G.wcond.wait() statement.

from threading import Thread, Condition
import time

class G:
    wcond = Condition()
    pcond = Condition()
    can_work = False
    can_process = True

class Worker(Thread):
    def run(self):
        while True:
            with G.wcond:
                while not G.can_work: # we wait for permission to work
                    G.wcond.wait()
                self.do_work()
                G.can_work = False
                with G.pcond: # we give permission to process
                    G.can_process = True
                    G.pcond.notify()

    def do_work(self):
        for i in range(3):
            print("working ...", self.name)
            time.sleep(0.2)


class Processor(Thread):
    def run(self):
        while True:
            with G.pcond:
                while not G.can_process: # we wait for permission to process
                    G.pcond.wait()
                self.do_process()
                G.can_process = False
                with G.wcond: # we give permission to work
                    G.can_work = True
                    G.wcond.notify()


    def do_process(self):
        for i in range(2):
            print("processing ...")
            time.sleep(0.2)

w, w2, p = Worker(), Worker(), Processor()
w.start()
w2.start()
p.start()

"""my output -->
processing ...
processing ...
('working ...', 'Thread-4')
('working ...', 'Thread-4')
('working ...', 'Thread-4')
processing ...
processing ...
('working ...', 'Thread-5')
('working ...', 'Thread-5')
('working ...', 'Thread-5')
processing ...
processing ...
('working ...', 'Thread-4')
('working ...', 'Thread-4')
etc
"""

With regard to your code, notice that

  1. The Thread's run() method must contain the thread's action, instead of its __init__() method.
  2. If the thread class redefines __init__(), it must call Thread.__init__(self) before running any substantial statement involving self. If you don't do that, you'll have mysterious bugs in your threads.

Edited by Gribouillis

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.