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

Python multithreaded programming

編輯:Python

This article is based on 【 Don't worry about it Python】Threading Learn to multithread Python Video of learning records .
Don't worry about it Python So handsome and cute hahaha .

Catalog

  • Add thread
  • join function —— Merge
  • Queue function —— Value passed
  • Thread lock

Function name function threading.active_count() How many threads are currently active threading.enumerate() Which threads are activated threading.current_thread() Which thread is running

Add thread

import threading
def thread_job():
print('This is an added Thread, number id %s' % threading.current_thread())
def main():
added_thread = threading.Thread(target=thread_job) # Create thread 
added_thread.start() # Start thread 
if __name__ == '__main__':
main()

join function —— Merge

First think about a few questions :

  1. How to merge a thread with the main thread ?
  2. If there are multiple threads , I want to wait for all threads to finish before running , How to achieve ?
import threading
import time
def thread_job():
print('T1 start')
for i in range(10):
time.sleep(0.1)
print('T1 finish')
def t2_job():
print('T2 start')
print('T2 finish')
def main():
added_thread = threading.Thread(target=thread_job, name='T1')
thread2 = threading.Thread(target=t2_job)
added_thread.start()
thread2.start()
added_thread.join()
thread2.join()
print('all down!')
if __name__ == '__main__':
main()

Queue function —— Value passed

You can pass parameters to the thread , But the thread cannot have a return value .
The purpose of the following example is to data All values in the list are squared :

import threading
import time
from queue import Queue
# List data to be modified 
data = [[1, 2, 3], [3, 4, 5], [4, 4, 4], [5, 5, 5]]
# Define a function , Square the values in the list 
def job(l):
for i in range(len(l)):
l[i] = l[i]**2
return l
# If you don't use multithreading , This can be achieved in the following ways 
def no_threading():
result = []
for d in data:
r = job(d)
result.append(r)
print(result)
def job2(l, q):
for i in range(len(l)):
l[i] = l[i]**2
q.put(l)
def multithreading():
q = Queue()
threads = []
# Start thread 
for i in range(4):
t = threading.Thread(target=job2, args=(data[i], q))
t.start()
threads.append(t)
# Recycle thread 
for thread in threads:
thread.join()
# get data 
results = []
for _ in range(4):
results.append(q.get())
print(results)
if __name__ == '__main__':
# no_threading()
multithreading()

Thread lock

import threading
def job1():
global A, lock
lock.acquire()
for i in range(10):
A += 1
print('job1', A)
lock.release()
def job2():
global A, lock
lock.acquire()
for i in range(10):
A += 10
print('job2', A)
lock.release()
if __name__ == '__main__':
A = 0
lock = threading.Lock()
t1 = threading.Thread(target=job1)
t2 = threading.Thread(target=job2)
t1.start()
t2.start()
t1.join()
t2.join()

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