1

How do I have a python script that contains functions say function1, function2, and function3 that I want to execute every 24 hours as a separate process/thread? So say...

Main Application executes function1, function2 function1 and function2 executes some long running process in a separate threads from the main application once function 1 finishes its task, reports back to main application that it completed

Main application prints "Done with Function 1." once function 2 finishes its task, reports back to main application that it completed

24 hours after function 1 finishes... the main app will execute it again 6 hours after function 2 finishes... the main app will execute the function again

Note that the functions would be doing similar things, but the data source is different, so having separate apps and doing a cron is undesirable. Examples would be great.

7
  • 2
    Have you tried import time? Commented Dec 10, 2012 at 1:32
  • How would the app do the "execute functions as a separate process that reports back to the main app (the executor), which would then know when to start counting to do another update. Note that I do not want a cron for this." Commented Dec 10, 2012 at 1:37
  • 1
    docs.celeryproject.org/en/latest/userguide/… Commented Dec 10, 2012 at 1:42
  • 3
    You might want to try the Advanced Python Scheduler Commented Dec 10, 2012 at 1:42
  • 1
    Why don't you want cron for this? Is there something it can't do that you need? And, if so, have you considered other schedulers that can do it? Or are you running on some kind of embedded-Python-only system with no underlying OS? Or… what? "Having separate apps" isn't a real problem—separate apps can all share a module with all those "functions… doing similar things". Commented Dec 10, 2012 at 1:52

2 Answers 2

3

If you need it inside the application, alarm() is a good place to start, and handler should fork() itself off. If you need to synchronize different functions at different intervals, PriorityQueue is great, as you can only maintain one alarm. There is a working alarm() example in the docs.

Otherwise, I'd probably go with separate applications, not functions, and trigger them by crontab.

Sign up to request clarification or add additional context in comments.

5 Comments

+1, and really, cron is probably what the OP wants, despite what he says… But one little nit: I'd suggest using multiprocessing instead of fork, especially if you want to "report back to the main app".
@abarnert: Eh, I guess I'm old school :P I didn't know about multiprocessing, but it looks like a wrapper for fork() with some niceties added (like interface for shmem). (I am not a Pythonista, so I don't know all the libs.) However, OP said he wants just "done with function1", for which fork() + SIGCHLD should suffice.
Well, besides the fact that it works on non-POSIX platforms like Windows, or POSIX platforms without SysV shmem, works with multi-computer grids, lets you actually put Python objects in shared memory (try doing that manually—I think that's how I lost my hair…), and abstracts many other things that would otherwise be complicated, yes, I guess you could say it's just a wrapper for fork(). But since one of the complicated things it abstracts is returning a value from a forked function, and the OP specifically asked for that and probably has no clue how to do it himself…
PS, as I said, I did +1 your answer, and I'm not trying to say you couldn't implement this properly with os.fork or anything, just that I wouldn't expect the OP to be able to do it given the level of knowledge his question implies, and that's why I'd recommend multiprocessing.
@abarnert: No, don't misunderstand me, multiprocessing is probably a better way anyway. I just didn't know about it, and thanks for pointing it out to me. (And obviously, as I noted, Python doesn't seem to have "standalone" shmem bindings other than in multiprocessing, and perhaps other niceties as well are there and there alone.)
0

Why go through the pain of setting up this as an application that starts the threads every hour. Why not a simple cron job (or if you are on a MS box, something similar) and each function writes its "done with functionX" into a log file.

Your crontab would look something like:

0 0 * * * function1.py >> log1
0 0 * * * function2.py >> log2

If you got a shebang in your script. If not, replace "function1.py" by "python function1.py".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.