Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use fork or threads?

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?

like image 859
Abhijeet Rastogi Avatar asked Mar 20 '10 12:03

Abhijeet Rastogi


People also ask

When should you not use threads?

If the sum of your threads is <100% of one core, then there is no reason whatsoever for you to use threads. If your program is not CPU bound, then you would be infinitely wiser to use an event loop1 or coroutines2. If you are not CPU bound, then the entire value proposition of threads does not apply to you.

What is the difference between a fork and a thread?

Threads are functions run in parallel, fork is a new process with parents inheritance. Threads are good to execute a task in parallel, while forks are independent process, that also are running simultaneously.

What is the difference between forking and multi threading?

A thread process is considered a sibling while a forked process is considered a child. Also, threads are known as light-weight processes as they don't have any overhead as compared to processes (as it doesn't issue any separate command for creating completely new virtual address space).

Does fork create a new thread or a new process?

The fork() system call in UNIX causes creation of a new process. The new process (called the child process) is an exact copy of the calling process (called the parent process) except for the following: The child process has a unique process ID.


2 Answers

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.

like image 157
Joe Carnahan Avatar answered Sep 19 '22 21:09

Joe Carnahan


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.

like image 37
Thomas Vander Stichele Avatar answered Sep 19 '22 21:09

Thomas Vander Stichele