Python multithreading is pseudo-multithreaded, at most one thread at a time is executing, but this does not make the code Python multithreading no effect, for IO-intensive systems, Python multi-threaded still can greatly improve performance ~
About Python pseudo multithreading can be used to understand the concept of Python Gil.
The following code relates to Python multi-threading, multi-process, process pool-related operations:
#encoding: Utf-8from multiprocessing import pool,manager,cpu_count,lock,processimport threadimport threadingdef Process_fun (msg):p rint ' process_fun msg: ', Msgpassdef thread_fun (msg):p rint ' thread_fun msg: ', msgpassif __name__ = = ' __ Main__ ': msg = ' Hello world '; #启动一个子进程msg = "is process" Child_proc = Process (Target=process_fun, args= (msg,)) Child_ Proc.start () #启动一个线程 use the thread module MSG = "is thread using the thread module" Thread.start_new_thread (Thread_fun, (msg,)) #启动一个线程 make Use the threading module MSG = "is thread using threading module" th = Threading. Thread (Target=thread_fun, args= (msg,)) Th.start () #进程池方式msg = "is pool process" Worker_count = 4pool = Pool (worker_count) For I in range (Worker_count):p Ool.apply_async (Process_fun, args= (msg,)) Pool.close () Pool.join () #主进程阻塞等待所有子进程执行完毕
The results of the implementation are as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A brief analysis of Python multi-process and multithreading