学习完线程,学习进程

进程和线程的语法有很多一样的地方,不过在操作系统中的差别确实很大。

模块是threading 和 multiprocessing

多进程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.

如何启动多进程

#Authon Ivor
from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())
    print("")

def f(name):
    info('\033[31;1mfunction f\033[0m')
    print('hello', name)

if __name__ == '__main__':
    info('\033[32;1mmain process line\033[0m')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()
View Code


进程间通信的三种方式

Queue

#Author:Ivor
from multiprocessing import Process,Queue
import os
# threading.queue.Queue()
def run(q):
    q.put("---in the Child process---")
    print("parent_pid:",os.getppid())
    print("current_pid:",os.getpid())
    print("------------------")


if __name__ == '__main__':
    q = Queue()
    print("---main process---")
    print("parent_pid:",os.getppid())
    print("current_pid:",os.getpid())
    print("------------------")
    p = Process(target=run,args=(q,))
    p.start()
    print(q.get())
    p.join()
View Code

相关文章: