Python Forum
Ouestion on Python Loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Ouestion on Python Loop (/thread-1502.html)



Ouestion on Python Loop - GOPI56 - Jan-08-2017

Hi All,
I have a question on this python which i have attached below. My question is i want the python script "PIRoncall.py" script to be run once but i want the read file loop to continue,currently it keeps on duplicating and there is mutiple process of "PIRoncall.py" script. I have thought of using shell command for checking process existence (pgrep?) before launching PIRoncall.py instance but i am not sure how to proceed. Anyone please help ?
import time
import subprocess
import os

oldline="none"
while True:
   fh=open("status_mjpeg.txt","r")
   line = fh.read(5)
   print line
   fh.close()
   time.sleep(0.3)
   if oldline !=line:
       if line == "md_re":
            print "on"
            time.sleep(0.2)
            os.system(' /usr/bin/python PIRoncall.py &')
            time.sleep (2)
       elif line == "ready":
            print "on"
            os.system(' /usr/bin/python PIRoffcall.py')
            time.sleep(2)
       oldline = line



RE: Ouestion on Python Loop - Windspar - Jan-08-2017

You are open and closing file in loop. So file reset it back to beginning.

import time
import subprocess
import os
 
def main():
    oldline="none"
    fh=open("status_mjpeg.txt","r")
    while True:
        line = fh.read(5)
        print line
        if(line == ""):
            break

        time.sleep(0.3)
        if oldline !=line:
             if line == "md_re":
                print "on"
                time.sleep(0.2)
                os.system(' /usr/bin/python PIRoncall.py &')
                time.sleep (2)
            elif line == "ready":
                print "on"
                os.system(' /usr/bin/python PIRoffcall.py')
                time.sleep(2)
            oldline = line

    fh.close()

main()



RE: Ouestion on Python Loop - buran - Jan-08-2017

Any particular reason no to import the RIPoncall and use it?