Python process and thread summary case analysis

The traditional way is to call 2 methods to perform 1 task, and the methods are executed in sequence

# - *- coding:utf-8-*-import threading
import time
def run(n):print('task',n)
 time.sleep(3)if __name__ =='__main__':run('t1')run('t2')

Multi-threaded example

2 Threads execute 1 task concurrently

# - *- coding:utf-8-*-import threading
import time
def run(n):print('task',n)
 time.sleep(3)if __name__ =='__main__':
 t1=threading.Thread(target=run,args=('t1',))
 t2=threading.Thread(target=run,args=('t2',))
 t1.start()
 t2.start()

Write your own class to inherit threading.Thread

# - *- coding:utf-8-*-import threading
import time
classMyThread(threading.Thread):
 def __init__(self,n):super(MyThread,self).__init__()
 self.n=n
 # There is a run method by default
 def run(self):print('runing task',self.n)if __name__ =='__main__':
 # In the main method through the object call thread
 t1=MyThread('t1')
 t2=MyThread('t2')
 t1.run()
 t2.run()

Use a for loop to start multiple threads

# - *- coding:utf-8-*-import threading
import time
def run(n):print('task',n)
 time.sleep(3)if __name__ =='__main__':for i inrange(10):
 t=threading.Thread(target=run,args=('t-%s'%i,))
 t.start()

Wait for the execution of multiple threads at the same time, and then execute other codes, because threads are running with other codes

# - *- coding:utf-8-*-import threading
import time
def run(n):print('task',n)
 time.sleep(3)if __name__ =='__main__':
 time_start=time.time()
 # Define an empty list to load the thread t instance
 t_objects=[]for i inrange(10):
 t=threading.Thread(target=run,args=('t-%s'%i,))
 t.start()
 t_objects.append(t)
 # After all threads are executed, execute the following code
 # Because the thread and the following code are running at the same time
 # If you want to wait for the thread to finish executing before executing other code
 # Use join()Method for blocking
 # End all t threads uniformly here
 for i in t_objects:
 t.join()
 time_end=time.time()
 sun=time_end-time_start
 print(sun)

to sum up

The above is an example analysis of python processes and threads introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave a message to me. The editor will reply to you in time!

Recommended Posts

Python process and thread summary case analysis
Python process and thread summary case analysis
Python uses Matlab command process analysis
FM algorithm analysis and Python implementation
Python file operation basic process analysis
Python file and directory operation code summary
Python replacement pip source method process analysis
Python and Go
Python basic summary
Python data analysis
A brief summary of the difference between Python2 and Python3
Python introspection and reflection
Summary: Ubuntu Python2.x and
Python processing json summary
[python] python2 and python3 under ubuntu
Python interview questions summary
Python advanced usage summary
Python3 configuration and entry.md
Python uses Matlab command process analysis
How to use python thread pool
Pycharm modify python path process diagram
Python file operation basic process analysis
Python process and thread summary case analysis
How to process excel table with python
Python replacement pip source method process analysis
Python process and thread summary case analysis