简体   繁体   中英

How to use use two loops parallel, Python?

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM