40

My program is designed in the following way:

  1. First part of the program takes real time values from a sensor and plots it using Matplotlib. This has to be done for long durations. And also, it logs information into a database.
  2. The second part is the IP Camera. I have to get the input from an IP Camera and display it. For displaying I am using OpenCV's imshow method. Also, I am storing the video from the IP Camera.

Question: I have the algorithms in place, the problem is I need to run both these in a while loops. The condition is that I cannot exit from any of them. Now threading is a good alternative for this but I have read about the GIL, so how do I go about running two infinite loops?

from multiprocessing import Process

def methodA():
    while TRUE:
        do something

def methodB():
    while TRUE:
        do something

p=Process(target=methodA())
p.start()
p1=Process(target=methodB())
p1.start()

Now when I start process p it starts executing, after that how do I start p1 to run simultaneously?

5
  • If your only concern is running infinite loops, there's no real problem using multithreading. Commented Apr 16, 2014 at 6:03
  • Also, once I start executing a thread, how do I execute the other thread when the first thread is running continuously/infinitely? Commented Apr 16, 2014 at 7:12
  • 1
    I don't understand what you mean. There are two different threads executing in parallel. Unless one is dependent on the other, you have no real issue. Commented Apr 16, 2014 at 7:28
  • One method runs infinitely, I start it in a thread, now how do I run another thread?. Wait, I shall add some code to shed light on the problem. Commented Apr 16, 2014 at 7:30
  • @gravetii Please see the updated question. I hope it helps. Commented Apr 16, 2014 at 7:34

2 Answers 2

72

As far as I understood your question, you have two different tasks that you want them to perform continuously. Now regarding your questions:

how do I go about running two infinite loops?

You can create two different threads that will run these infinite loops for you. The first thread will perform your task1 and second one will perform task2.

Also, once I start executing a thread, how do I execute the other thread when the first thread is running continuously/infinitely?

If you are using two different threads then you don't need to be worried about this issue. If the threads are not sharing any resource then you don't need to worry about this fact. How ever if you want to stop/pause one thread from the other thread or vice versa then you can implement a mechanism using flags or locks. These questions will help in this case:

Is there any way to kill a Thread in Python?

Why does the python threading.Thread object has 'start', but not 'stop'?

making-a-program-munltithreaded

Sample example using threading:

from threading import Thread

class myClassA(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'A'

class myClassB(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'B'


myClassA()
myClassB()
while True:
    pass

For shared resources?

Use Locks for them. Here are some examples. One, two and How to synchronize threads in python?

what if I don't want to run it using classes? How do I do this using only methods?

from threading import Thread

def runA():
    while True:
        print 'A\n'

def runB():
    while True:
        print 'B\n'

if __name__ == "__main__":
    t1 = Thread(target = runA)
    t2 = Thread(target = runB)
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()
    while True:
        pass
Sign up to request clarification or add additional context in comments.

5 Comments

Please check the updated question if you have not done so, it'll explain the situation more clearly.
And what do I do if they share resources?
Wow! Thanks for the answer. I tried and it works perfectly! Just some questions, why are you passing Thread to the class? And what if I don't want to run it using classes? How do I do this using only methods?
upvoted! how do I pause them and resume them or monitor them?
instead of an infinite loop at the end join method call with each thread will be optimal. t1.join(), t2.join()
5
    from threading import Thread
import time

class PrintA(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.running = True

    def run(self):
        while self.running:
            print('A')
            time.sleep(1)
    def stop(self):
        self.running = False

class PrintB(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.running = True
    def run(self):
        while self.running:
            print('B')
            time.sleep(2)
    def stop(self):
        self.running = False

a = PrintA()
b = PrintB()

a.start()
b.start()

time.sleep(10)
a.stop()
time.sleep(10)
b.stop()

1 Comment

very well done, but is there a way to reuse this thread after it is finished once?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.