views:

245

answers:

5

Hi, I am trying to make 2 functions run at the same time.

def func1():
    print 'Working'

def func2():
    print 'Working'

func1()
func2()

Does anyone know how to do this?

+3  A: 

Do this:

import threading
from threading import Thread

def func1():
    print 'Working'

def func2():
    print 'Working'

if __name__ == '__main__':
    Thread(target = func1).start()
    Thread(target = func2).start()
chrissygormley
He might want to know that because of the Global Interpreter Lock they will not execute at the exact same time even if the machine in question has multiple CPUs. http://wiki.python.org/moin/GlobalInterpreterLock
Jonas Elfström
Is this code correct? It is calling twice the two functions.
joaquin
@joaquin -- Your right, sorry forgot to take the function exectutions out after copy and paste.
chrissygormley
A: 

What does the "same time" mean?

shingle
+2  A: 

The answer about threading is good, but you need to be a bit more specific about what you want to do.

If you have two functions that both use a lot of CPU, threading (in CPython) will probably get you nowhere. Then you might want to have a look at the multiprocessing module or possibly you might want to use jython/IronPython.

If CPU-bound performance is the reason, you could even implement things in (non-threaded) C and get a much bigger speedup than doing two parallel things in python.

Without more information, it isn't easy to come up with a good answer.

Mattias Nilsson
A: 

this Can be Done with The Help Of Threading.... here i m using Java...

import java.lang.*;                  // imports the packages
class Viz 
{
    public static void main(String[] args)
    {
        Thread t1 = new Run1();
        Thread t2 = new Run2();
        t1.start();
        t2.start();
    }
}
class Run1 extends Thread
{
    public run()                        // your Func1
    {
        -----------
        -----------
    }
}
// the Name of Func Must Be "run()"....
class Run2 extends Thread
{
    public run()                        // your Func2
    {
        -----------
        -----------
    }
}
Vizay Soni
The Python threading differ from Java threads a lot in some aspects. Others have mentioned the GIL, which is a good example of differences.Also, shouldn't you join() your threads?
Mattias Nilsson
How does showing a java example help answer a python question?
Bryan Oakley
OOPs, i didnt See Python....@Mattias : if i join the Thread that means one thread joins other...so one runs after others...but in question it says "run at the same time"...thanx..
Vizay Soni
I meant after you start the two. Otherwise your main method will return while threads are still running.
Mattias Nilsson
A: 

In python 2.6 and 3 you can use also the multiprocessing module.

multiprocessing is a package that supports spawning processes using an API similar to the threading module. The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. It runs on both Unix and Windows.

You have additional examples here. For each function you make:

func1 = multiprocessing.Process(target=myfunction, args=(myarg1,))
func1.start()
joaquin