views:

166

answers:

5

In my script, I have a function foo which basically uses pynotify to notify user about something repeatedly after a time interval say 15 minutes.

def foo:
    while True:
        """Does something"""
        time.sleep(900)

My main script has to interact with user & does all other things so I just cant call the foo() function. directly.

Whats the better way of doing it and why? Using fork or threads?

A: 

Processes are much simpler. Just turn them loose and let the OS handle it.

Also, processes are often much more efficient. Processes do not share a common pool of I/O resources; they are completely independent.

Python's subprocess.Popen handles everything.

S.Lott
+1 from me - S. Lott is far more authoritative than me, especially when it comes to Python.
duffymo
A: 

If by fork you mean os.fork then I would avoid using that. It is not cross platform and too low level - you would need to implement communication between the processes yourself.

If you want to use a separate process then use either the subprocess module or if you are on Python 2.6 or later the new multiprocessing module. This has a very similar API to the threading module, so you could start off using threads and then easily switch to processes, or vice-versa.

For what you want to do I think I would use threads, unless """does something""" is CPU intensive and you want to take advantage of multiple cores, which I doubt in this particular case.

Dave Kirby
shadyabhi
+4  A: 

I won't tell you which one to use, but here are some of the advantages of each:

Threads can start more quickly than processes, and threads use fewer operating system resources than processes, including memory, file handles, etc. Threads also give you the option of communicating through shared variables (although many would say this is more of a disadvantage than an advantage - See below).

Processes each have their own separate memory and variables, which means that processes generally communicate by sending messages to each other. This is much easier to do correctly than having threads communicate via shared memory. Processes can also run truly concurrently, so that if you have multiple CPU cores, you can keep all of them busy using processes. In Python*, the global interpreter lock prevents threads from making much use of more than a single core.


* - That is, CPython, which the implementation of Python that you get if you go to http://python.org and download Python. Other Python implementations (such as Jython) do not necessarily prohibit Python from running threads on multiple CPUs simultaneously. Thanks to @EOL for the clarification.

Joe Carnahan
shadyabhi
You probably mean "In CPython, the global interpreter lock…". In fact, there is no GIL in a few other Python implementations (Jython, Iron Python,…).
EOL
@EOL - True enough. I assumed that the questioner was using CPython, since people usually specify if they're using anything *other* than CPython. Point taken, though - I'll update my answer.
Joe Carnahan
+2  A: 

For these kinds of problems, neither threads nor forked processes seem the right approach. If all you want to do is to once every 15 minutes notify the user of something, why not use an event loop like GLib's or Twisted's reactor ? This allows you to schedule operations that should run once in a while, and get on with the rest of your program.

Thomas Vander Stichele
I am a beginner so I dont have idea about how do I implement it. But, your answer seems convincing...
shadyabhi
+2  A: 

Using multiple processes lets you exploit multiple CPU cores at the same time, while, in CPython, using threads doesn't (threads take turns using a single CPU core) -- so, if you have CPU intensive work and absolutely want to use threads, you should consider Jython or IronPython; with CPython, this consideration is often enough to sway the choice towards the multiprocessing module and away from the threading one (they offer pretty similar interfaces, because multiprocessing was designed to be easily put in place in lieu of threading).

Net of this crucial consideration, threads might often be a better choice (performance-wise) on Windows (where making a new process is a heavy task), but less often on Unix variants (Linux, BSD versions, OpenSolaris, MacOSX, ...), since making a new process is faster there (but if you're using IronPython or Jython, you should check, on the platforms you care about, that this still applies in the virtual machines in question -- CLR with either .NET or Mono for IronPython, your JVM of choice for Jython).

Alex Martelli