0

I am working on motion detection application in Raspberry Pi. There is a python script which should check output of c++ code and if "alert" is printed, it should start to upload images from a given directory to server:

for line in self.output:
  if line == "alert\n":
    # upload a frame from directory to a server

However, each frame takes around 30 seconds to upload to a server, but each new line is printed out in 500ms. So, it is not efficient at all to wait for uploading current frame in each if condition of for loop.

I am new to Python. Is there any way to run two methods parallel? I know about threads, but I am not sure what would happen if for loop creates new thread each time in Raspberry Pi.

korujzade
  • 400
  • 4
  • 23
  • You should start a **single** new thread and have a queue of operations for it to complete. This means that the operations will complete in sequence without blocking your main thread. – Luke Joshua Park Dec 23 '15 at 05:09
  • follow this link http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies – Serjik Dec 23 '15 at 05:10

1 Answers1

0

each time you execute a the if statement a thread will be created, it would execute and then stop(if the thread itself doesn't have a loop). you can use the following syntax.

    def myFunc():
         #does the stuff you want it to do 
    for line in self.output:
       if line == "alert\n":
          threading.Thread(target=myFunc,args=randomArgs)
abhsag
  • 1
  • 2