Search code examples
pythonmulticore

Running methods on different cores on python


Is there any easy way to make 2 methods, let's say MethodA() and MethodB() run in 2 different cores? I don't mean 2 different threads. I'm running in Windows, but I'd like to know if it is possible to be platform independent.

edit: And what about

http://docs.python.org/dev/library/multiprocessing.html and parallel python ?


Solution

  • You have to use separate processes (because of the often-mentioned GIL). The multiprocessing module is here to help.

    from multiprocessing import Process
    from somewhere import A, B 
    if __name__ == '__main__':
        procs = [ Process(target=t) for t in (A,B) ]
    
        for p in procs: 
            p.start()
    
        for p in procs: 
            p.join()