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?
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?
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()
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.
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
{
-----------
-----------
}
}
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()