【Python】多线程#181101

多线程

import threading

def main():
	print(threading.active_count())
	print(threading.enumerate())
	print(threading.current_thread())

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
1
[<_MainThread(MainThread, started 4124)>]
<_MainThread(MainThread, started 4124)>
线程数
print(threading.active_count())

线程名
print(threading.enumerate())

当前线程数
print(threading.current_thread())

添加线程
added_thread = threading.Thread()
added_thread.start()

添加线程add thread

import threading

def thread_job():
	print("This is an added Thread,number is %s"% threading.current_thread())

def main():
	added_thread = threading.Thread(target =thread_job)
	added_thread.start()

if __name__ == '__main__':
	main()
import threading
import time

def thread_job():
	print('T1 start\n')
	for i in range(10):
		time.sleep(0.1)
	print('T1 finish\n')

def main():
	added_thread = threading.Thread(target =thread_job,name = 'T1')
	added_thread.start()
	print('all done\n')

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
T1 start

all done

T1 finish

join功能

等待
added_thread.join()
import threading
import time

def thread_job():
	print('T1 start\n')
	for i in range(10):
		time.sleep(0.1)
	print('T1 finish\n')

def main():
	added_thread = threading.Thread(target =thread_job,name = 'T1')
	added_thread.start()
	added_thread.join()
	print('all done\n')

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
T1 start

T1 finish

all done
  • 比较
import threading
import time

def thread_job():
	print('T1 start\n')
	for i in range(10):
		time.sleep(0.1)
	print('T1 finish\n')

def T2_job():
	print('T2 start\n')
	print('T2 finish\n')

def main():
	added_thread = threading.Thread(target = thread_job,name = 'T1')
	thread2 = threading.Thread(target =T2_job,name = 'T2')
	added_thread.start()
	thread2.start()
	print('all done\n')

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
T1 start

T2 start

T2 finish

all done

T1 finish
import threading
import time

def thread_job():
	print('T1 start\n')
	for i in range(10):
		time.sleep(0.1)
	print('T1 finish\n')

def T2_job():
	print('T2 start\n')
	print('T2 finish\n')

def main():
	added_thread = threading.Thread(target = thread_job,name = 'T1')
	thread2 = threading.Thread(target =T2_job,name = 'T2')
	added_thread.start()
	thread2.start()
	added_thread.join()
	print('all done\n')

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
T1 start

T2 start

T2 finish

T1 finish

all done
import threading
import time

def thread_job():
	print('T1 start\n')
	for i in range(10):
		time.sleep(0.1)
	print('T1 finish\n')

def T2_job():
	print('T2 start\n')
	print('T2 finish\n')

def main():
	added_thread = threading.Thread(target = thread_job,name = 'T1')
	thread2 = threading.Thread(target =T2_job,name = 'T2')
	added_thread.start()
	thread2.start()
	thread2.join()
	print('all done\n')

if __name__ == '__main__':
	main()

D:\PythonTest>python test.py
T1 start

T2 start

T2 finish

all done

T1 finish

queue功能

import threading
import time
from queue import Queue

def job(l,q):
	for i in range(len(l)):
		l[i] = l[i] ** 2
	q.put(l)

def multithreading():
	q = Queue()
	threads = []
	data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
	for i in range(4):
		t = threading.Thread(target = job,args = (data[i],q))
		t.start()
		threads.append(t)
	for thread in threads:
		thread.join
	results = []
	for _ in range(4):
		results.append(q.get())
	print(results)


if __name__ == '__main__':
	multithreading()

GIL

import threading
import time
from queue import Queue
import copy

def job(l,q):
	res = sum(l)
	q.put(res)

def multithreading(l):
	q = Queue()
	threads = []
	for i in range(4):
		t = threading.Thread(target = job,args = (copy.copy(l),q),name = 'T%i'%i)
		t.start()
		threads.append(t)
	[t.join() for t in threads]
	total = 0
	for _ in range(4):
		total += q.get()
	print(total)

def normal(l):
	total = sum(l)
	print(total)

if __name__ == '__main__':
	l = list(range(1000000))
	s_t = time.time()
	normal(l * 4)
	print('normal:',time.time() - s_t)
	s_t = time.time()
	multithreading(l)
	print('multithreading:',time.time() - s_t)

D:\PythonTest>python test.py
1999998000000
normal: 0.13805341720581055
1999998000000
multithreading: 0.16271591186523438

锁loop

import threading
import time
from queue import Queue

def job1():
	global A
	for i in range(10):
		A += 1
		print('join1',A)

def job2():
	global A
	for i in range(10):
		A += 10
		print('join2',A)

if __name__ == '__main__':
	A = 0
	t1 = threading.Thread(target = job1)
	t2 = threading.Thread(target = job2)
	t1.start()
	t2.start()
	t1.join()
	t2.join()

D:\PythonTest>python test.py
join1 1
join1 2
join1 13
join1 14
join1 15
join1 16
join1 17
join1 18
join1 19
join1 20
join2 12
join2 30
join2 40
join2 50
join2 60
join2 70
join2 80
join2 90
join2 100
join2 110
import threading
import time
from queue import Queue

def job1():
	global A,lock
	lock.acquire()
	for i in range(10):
		A += 1
		print('join1',A)
	lock.release()

def job2():
	global A,lock
	lock.acquire()
	for i in range(10):
		A += 10
		print('join2',A)
	lock.release()

if __name__ == '__main__':
	lock = threading.Lock()
	A = 0
	t1 = threading.Thread(target = job1)
	t2 = threading.Thread(target = job2)
	t1.start()
	t2.start()
	t1.join()
	t2.join()

D:\PythonTest>python test.py
join1 1
join1 2
join1 3
join1 4
join1 5
join1 6
join1 7
join1 8
join1 9
join1 10
join2 20
join2 30
join2 40
join2 50
join2 60
join2 70
join2 80
join2 90
join2 100
join2 110

转载于:https://my.oschina.net/hellopasswd/blog/2396054

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值