A brief analysis of Python multi-process and multithreading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.