程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of Python multithreaded multiprocess task

編輯:Python

1. What is multitasking ?

Multitasking refers to executing multiple tasks at the same time , And it is divided into concurrency and parallelism .

Concurrent : Perform multiple tasks alternately over a period of time , The number of tasks is greater than CPU The number of core

parallel : Actually perform multiple tasks together over a period of time  

  A running program is a process

Multi process , It can be understood as downloading multiple tasks at the same time Executing multiple processes at the same time Be able to make full use of CPU resources Increase of efficiency

2. Process creation steps

# 1. Import process package
import multiprocessing
# 2. Create process objects through process classes
# Process object = multiprocessing.Process(target= Task name )
# 3. Start the process and execute the task
# Process object .start()

2.1  Get the process number

# There are two ways to get the process number
# 1. Get the current process number os.getpid()
"""
import os
pid = os.getpid()
print(pid)
"""
# 2. Get the current parent process number os.getppid()
"""def work():
print("work Parent process number :", os.getppid())
"""

3. Steps to create a thread ( The creation step of a thread is very similar to that of a process )

# 1. Import thread module
import threading
# 2. Create thread objects through thread classes
# Process object = threading.Thread(target= Task name )
# 3. Start thread object
# Process object .start()

3.1 Get the thread object of the current thread

  Be careful : Execution between multiple threads is out of order , By CPU Transfer decision

import threading
import time
def task():
# Add delay Delay 1 second
time.sleep(1)
# curren_thread Get the thread object of the current thread
thread = threading.current_thread()
print(thread)
if __name__ == '__main__':
for i in range(5):
sub_thread = threading.Thread(target=task)
sub_thread.start()

4. Multi process ( Multithreading ) Execute tasks with parameters

args Pass parameters to the execution task in the form of tuples args=(3,) You must have this comma

kwargs Pass parameters to the execution task in a dictionary kwargs={"num":3}

Let me use an example to illustrate

4.1  Multiple processes execute tasks with parameters

# 1. Import process package
import multiprocessing
import time
# Sing a song
def sing(num, name):
# for+ Custom iteration traversal name +in+ List name :
# num It's a parameter
for i in range(num):
print(name)
print(" Sing a song ..")
# Delay 0.5 second
time.sleep(0.5)
# dance
def dance(num, name):
for i in range(num):
print(name)
print(" dance ...")
# Delay 0.5 second
time.sleep(0.5)
if __name__ == '__main__':
# 2. Create process objects through process classes
# Process object = multiprocessing.Process(target= Task name )
# target: Specify the name of the function executed by the process
# ars: Use tuple method to pass parameters to the specified task Parameters are passed in order
# kwargs: Use the dictionary method to pass parameters to the specified task Parameters are passed by key Key and parameter names should be consistent
sing_process = multiprocessing.Process(target=sing, args=(3, "xiaoming"))
dance_process = multiprocessing.Process(target=dance, kwargs={"num": 2, "name": "xiaoming"})
# 3. Start the process and execute the task
# Process object .start()
sing_process.start()
dance_process.start()
# multitasking Parallel multiple processes to complete multiple tasks 

4.2  Multithreading performs tasks with parameters

The above conditions are unchanged Just put it

Multi process multiprocessing.Proces Turned into Multithreading threading.Thread

if __name__ == '__main__':
# sing()
# dance()
# 2. Create thread objects through thread classes args Tuple mode parameter transmission should be consistent with the order of parameters
sing_thread = threading.Thread(target=sing, args=(3, "xiaoming"))
# here kwargs The key of the dictionary should be the same as the parameter name
dance_thread = threading.Thread(target=dance, kwargs={"count": 2})
# 3. Start thread object
sing_thread.start()
dance_thread.start()
# The main process will wait until all child processes are finished 

5. Set up the daemons ( The main thread )

Both the main process and the main thread will wait for all child processes ( Sub clue ) After that , Will end .

If you want the main process ( The main thread ) After the end , Subprocesses ( Sub clue ) No more execution , You need to set up a guard .

5.1  Set the format of the main daemon :

# Sub object .daemon = True

The following example :

# The main process will wait until all child processes are finished
import time
import multiprocessing
def work():
# Subprocess work 2 second
for i in range(10):
print(" In the work ")
time.sleep(0.2)
if __name__ == '__main__':
# Create child process
work_process = multiprocessing.Process(target=work)
# Set up the daemons Once the main process exits The child process will also exit No longer execute subprocess code
# Sub object .daemon = True
work_process.daemon = True
work_process.start()
# Main process sleep 1 second
time.sleep(1)
print(" Main process execution completed ....")

5.2  There are two formats for setting the guard main thread :

# Sub object .setDaemon(True) or 
# Sub object = threading.Thread(target=work, demon=True)
import time
import threading
The following example
def work():
for i in range(10):
print(" Work ..")
time.sleep(0.2)
if __name__ == '__main__':
# Set up the guardian main thread The main thread will no longer wait for all child processes to finish
# sub_thread = threading.Thread(target=work, demon=True)
sub_thread = threading.Thread(target=work)
# Sub object .setDaemon(True)
sub_thread.setDaemon(True)
sub_thread.start()
# Main thread waiting 1 Seconds later
time.sleep(1)
print(" The main thread is over ...")

6. Process versus thread

 Relationship comparison
1. Threads are attached to processes , No process, no thread
2. A process provides a thread by default , Processes can create multiple threads
Difference and contrast
1. The resource cost of creating a process is greater than that of creating a thread
2. Process is the basic unit of operating system resource allocation , Thread is CPU The basic unit of dispatch
3. Thread cannot execute independently , Must be in the process of existence
Comparison of advantages and disadvantages :
Process advantages and disadvantages You can use multi-core Resources are expensive
Advantages and disadvantages of threads Multi core cannot be used Resource overhead 

If there is anything unclear, please give me your advice   thank *.*


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved