I need to start the execution of a function foo() every 10 seconds, and the foo() function takes a random time between 1 and 3 seconds to be executed.  import time import datetime  def foo():     # ... code which takes a random time between 1 and 3 seconds to be executed  while True:     foo()     time.sleep(10)   of course in the above example the function foo() is not executed every 10 seconds but every 10 + random seconds.  Is there a way to start executing foo() each exactly 10 seconds? I'm sure it's something related with threading but can't find a proper example.
4303
I want to repeatedly execute a function in Python every 60 seconds forever (just like an  in Objective C). This code will run as a daemon and is effectively like calling the python script every minute using a cron, but without requiring that to be set up by the user.  In , the solution appears to effectively just  for x seconds. I don't need such advanced functionality so perhaps something like this would work  while True:     # Code executed here     time.sleep(60)   Are there any foreseeable problems with this code?
14010
I'm looking for a library in Python which will provide at and cron like functionality.  I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron.  For those unfamiliar with cron: you can schedule tasks based upon an expression like:    0 2 * * 7 /usr/bin/run-backup # run the backups at 0200 on Every Sunday  0 9-17/2 * * 1-5 /usr/bin/purge-temps # run the purge temps command, every 2 hours between 9am and 5pm on Mondays to Fridays.   The cron time expression syntax is less important, but I would like to have something with this sort of flexibility.   If there isn't something that does this for me out-the-box, any suggestions for the building blocks to make something like this would be gratefully received.  Edit I'm not interested in launching processes, just "jobs" also written in Python - python functions. By necessity I think this would be a different thread, but not in a different process.  To this end, I'm looking for the expressivity of the cron time expression, but in Python.   Cron has been around for years, but I'm trying to be as portable as possible. I cannot rely on its presence.
13304
I would like to know how to put a time delay in a Python script.
35834
I ran this command to try and get my python program to run in the background and let me exit the SSH connection I am using:  nohup python files.py &gt;&gt; files.log &amp;   I then check ps to check it worked   $ ps a   PID TTY      STAT   TIME COMMAND 13059 pts/0    Ss     0:00 -bash 13327 pts/0    R+     0:00 ps a [1]+  Exit 1                  nohup python files.py &gt;&gt; files.log   What does this [1]+  Exit 1 mean?  And how can I run my program in the background letting me exit correctly?
74496
Please advise how to pass parameters into a function called using setInterval.  My example setInterval(funca(10,3), 500); is incorrect.
19675
and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With Python, sometimes the approaches are somewhat kludgey - i.e., adding timing code to __main__.  What is a good way to profile how long a Python program takes to run?
27583
documentation says that      Maximum execution time for each Apex transaction is 10 minutes for   both Synchronous and Asynchronous   To understand this I executed the below code in anonymous apex window :  Long startTime = System.now().getTime(); Long maxLimit = 9*60*1000; //I am taking max limit as 9 mins. while(System.now().getTime() - startTime &lt;= maxLimit ){   }   I get this error :    What is one use case where we don't run into CPU time limit(or other limits) but reach execution time limit?
41693
What's the proper way to execute a function every 3 months? Watchdog variables don't seem to be available after a certain time. I could create another table to insert the execute function date to compare, but it doesn't seem to be the best approach.  The functionality of such functions are:   Send an eMail Tag an inactive user
79666
Question  I'd like to be able to run a UNIX command precisely every second over a long time period.  I need a solution, which does not lag behind after a certain time, because of the time the command itself needs for execution. sleep, watch, and a certain python script all failed me in this regard.  On the microcontroller's such as the  I'd do that through hardware clock interrupts. I'd like to know whether there is a similar time-precise shell script solution. All the solutions which I found within StackExchange.com, resulted in a noticeable time lag, if run over hours. See details below.  Practical purpose / application  I want to test whether my network connection is continuously up by sending timestamps via nc (netcat) every 1 second.  Sender:  precise-timestamp-generator | tee netcat-sender.txt | nc $receiver $port   Receiver:  nc -l -p $port &gt; netcat-receiver.txt   After completion, compare the two logs:  diff netcat-sender.txt netcat-receiver.txt   The diffs would be the untransmitted timestamps. From this I would know at what time my LAN / WAN / ISP makes troubles.     Solution SLEEP  while [ true ]; do date "+%Y-%m-%d %H:%M:%S" ; sleep 1; done | tee timelog-sleep.txt   Gets a certain offset over time, as the command within the loop also takes a little time.  Precision  cat timelog-sleep.txt  2012-07-16 00:45:16 [...] 2012-07-16 10:20:36   Seconds elapsed: 34520  wc -l timelog-sleep.txt   Lines in file: 34243  Precision summarized:   34520-34243 = 277 timing problems 34520/34243 = 1.008 = 0.8 % off   Solution REPEAT PYTHON  Found at:   repeat.py 1 "date '+%Y-%m-%d %H:%M:%S'" &gt;&gt; timelog-repeat-py.txt   Supposed to avoid the time offset, but fails to do so.  Precision  wc -l timelog-repeat-py.txt  2012-07-16 13:42:44 [...] 2012-07-16 16:45:24   Seconds elapsed: 10960  wc -l timelog-repeat-py.txt   Lines in file: 10859  Precision summarized:   10960-10859 = 101 timing problems 10960/10859 = 1.009 = 0.9 % off   Solution WATCH  watch -n 1 "date '+%Y-%m-%d %H:%M:%S' &gt;&gt; ~/Desktop/timelog-watch.txt"   Precision  wc -l timelog-watch.txt 2012-07-16 11:04:08 [...] 2012-07-16 13:25:47   Seconds elapsed: 8499  wc -l timelog-watch.txt   Lines in file: 8366  Precision summarized:   8499-8366 = 133 timing problems. 8499/8366 = 1.016 = 1.6 % off.
56244
I have a method that I would like to call.  However, I'm looking for a clean, simple way to kill it or force it to return if it is taking too long to execute.  I'm using Java.  to illustrate:  logger.info("sequentially executing all batches..."); for (TestExecutor executor : builder.getExecutors()) { logger.info("executing batch..."); executor.execute(); }   I figure the TestExecutor class should implement Callable and continue in that direction.  But all i want to be able to do is stop executor.execute() if it's taking too long.  Suggestions...?  EDIT  Many of the suggestions received assume that the method being executed that takes a long time contains some kind of loop and that a variable could periodically be checked. However, this is not the case.  So something that won't necessarily be clean and that will just stop the execution whereever it is is acceptable.
22263
How can I repeat a command every interval of time , so that it will allow  me to run commands for checking  or monitoring  directories ?   There is no need for a script, i need just a simple command to be executed in terminal.
3740
I found 11 questions like this on gamingSE already, but none of them have proper and up-to-date answers. So I'm creating this Q&amp;A:  How do you either activate a command x seconds/minutes/hours/… in the future or every x seconds/minutes/… in a loop?  Repeating command blocks and ticking functions execute 20 times per second, redstone is  in many situations and most of the extremely fancy solutions, like falling blocks in cobwebs, have broken over time or are needlessly complicated.
19248
I have a form with an &lt;input type=text /&gt; and I want to call a javascript function after 5 seconds of the last key press, and every time a new key is pressed, this timer should reset and only call the function after 5 seconds.  How can I do this?  I'm using jQuery.  thanks!
90516
I'm trying to understand the basic concept of random processes.I already understood that a continuous-time random process is defined by X(¥,t) where ¥ is each element in the Sample Space and t is the time index.     By this definition, we can see the random process as an ensemble of sample realizations ( time-functions defined for each outcome in the Sample Space) or as a set of random variables ( each one defined for a specific time t ). In my mind, the value of the time-function ( sample realizations ) X(¥1,t) reflects the changing value of the probability of outcome ¥1 being choosen in the sample space, is that correct ?    For example, we have a boy B in the sample space representing outcome ¥1.Having this outcome ¥1 at time t=0 equals 30%.As the time passes, the sample spaces gets more crowded and at time t=5, the probabily of choosing boy B equals 20%.   Can i think that the time-function X(¥1,t) will equal 0,3 for t=0 and 0,2 for t=5 ? Does X(¥1,0)=0,3      and     X(¥1,5)=0,2         ?  Thanks in advance
31773
I'm more of a programmer than a statistician, so I hope this question isn't too naive.  It happens in sampling program executions at random times. If I take N=10 random-time samples of the program's state, I could see function Foo being executed on, for example, I=3 of those samples. I'm interested in what that tells me about the actual fraction of time F that Foo is in execution.  I understand that I is binomially distributed with mean F*N. I also know that, given I and N, F follows a beta distribution. In fact I've verified by program the relationship between those two distributions, which is  cdfBeta(I, N-I+1, F) + cdfBinomial(N, F, I-1) = 1   The problem is I don't have an intuitive feel for the relationship. I can't "picture" why it works.  EDIT: All the answers were challenging, especially @whuber's, which I still need to grok, but bringing in order statistics was very helpful. Nevertheless I've realized I should have asked a more basic question: Given I and N, what is the distribution for F? Everyone has pointed out that it's Beta, which I knew. I finally figured out from Wikipedia () that it appears to be Beta(I+1, N-I+1). After exploring it with a program, it appears to be the right answer. So, I would like to know if I'm wrong. And, I'm still confused about the relationship between the two cdfs shown above, why they sum to 1, and if they even have anything to do with what I really wanted to know.
65688
In a bash script sometimes you need the user to wait some seconds for a background process to finish.  I usually use for example:  sleep 10   How can I add a kind of progressbar to the script, so the user knows how long to wait?  I installed the command bar but I don't understand the manual.
36511
I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this:  var myCallBackExample = {     myFirstFunction : function( param1, param2, callback ) {         // Do something with param1 and param2.         if ( arguments.length == 3 ) {             // Execute callback function.             // What is the "best" way to do this?         }     },     mySecondFunction : function() {         myFirstFunction( false, true, function() {             // When this anonymous function is called, execute it.         });     } };   In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.