Avatar of fixitben
fixitben

asked on 

timed loop in python

How would you create a timed loop that never stops.

Bascially I want to do this

#1
time(60)
run script
goto #1

But I don't want it to stop everything while it is doing this. I just want it to run silently in the back ground.

Thanks
Fixitben
Scripting LanguagesPythonProgramming Languages-Other

Avatar of undefined
Last Comment
fixitben
Avatar of pepr
pepr

import time

while True:
    print time.strftime('%X')  # some action
    time.sleep(2)              # two seconds to make it more visible
Avatar of ramrom
ramrom
Flag of United States of America image

"But I don't want it to stop everything while it is doing this. I just want it to run silently in the back ground."

Please clarify the requirement. Especially these phrases:
"stop everything"
"run silently"  
"in the back ground"
Avatar of pepr
pepr

If your script to be run in the loop is the Python script, then you should put its body into definition of a function. The main() is used usually for the purpose. The usual way to have a script that can be also run as a module is to test for the __name__ attribute:

myscript.py
===================
def main():
    import time
    print time.strftime('%X')


if __name__ == '__main__':
    main()
===================

This way the body inside the main() will be called when the script is executed from command line. But the same body can be executed as a function from inside a module through call myscript.main():

===================
import time
import myscript

while True:
    myscript.main()
    time.sleep(2)    # two seconds to make it more visible
===================

If the script to be executed in the loop is not in Python, you can use the os.system() to launch the system command interpreter (shell or cmd):

===================
import time
import os

while True:
    os.system('echo 123')
    time.sleep(2)    # two seconds to make it more visible
===================



Avatar of pepr
pepr

In Unix, use normal way to put the executed script to the background. In Windows, run the script.py not through the python.exe, but through pythonw.exe. For the last example, you will see the cmd window flashing because of the echo command displayed in the cmd window. When no visible output is produced, you will not see anything on the display.
Avatar of fixitben
fixitben

ASKER

Well this is what i have.  But when it runs it stops everything, I would like to spawn this as a child.  So it runs by it's self indpendent of the parent. So the parent can continue and let this one just keep running.

def timers():

      
      while (1):
            import nt
            import time
            print "begin"
            c1 = nt.times()
            cpu = c1[0] + c1[1]
            print cpu
            #nt.system('start cmd')
            print "exiting"
            time.sleep(20)

timers()
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of fixitben
fixitben

ASKER

Ok let me explain this a little better.  We run program called abaqus the back end of it is python.  We have the ablity to run scripts when it starts up.  But When I run the above script it is spawned inline I am assuming so it waits while the count down occurs and locks up the program.  The for the split second that it checks the cpu it frees up the program.  So what I would like to do is spawn this script as a sperate thread, so that it doesn't stop the current program.

Thanks
Fixitben
SOLUTION
Avatar of ramrom
ramrom
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of fixitben
fixitben

ASKER

Well those helped me get to the answer. Thanks for ya'lls help.

Thanks
Fixiten
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo