一 多进程multiprocessing

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.

import multiprocessing,time

def run(name):
    print("hello",name)
    time.sleep(2)

if __name__ == '__main__':

    for i in range(10):
        p = multiprocessing.Process(target=run,args=('Bob %s'%i,))
        p.start()
import multiprocessing,time,threading

def thread_run():
    print(threading.get_ident()) #线程号

def run(name):
    print("hello",name)
    t = threading.Thread(target=thread_run,)
    t.start()
    time.sleep(2)

if __name__ == '__main__':

    for i in range(10):
        p = multiprocessing.Process(target=run,args=('Bob %s'%i,))
        p.start()
可以在进程中起线程

相关文章: