Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ouestion on Python Loop
#1
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
Reply
#2
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()
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Any particular reason no to import the RIPoncall and use it?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020