最近需要使用 python3 多线程处理大型数据,顺道探究了一下,python3 的线程模型的情况,下面进行简要记录;
多线程运行的优点:
内核线程:由操作系统内核创建和撤销;
用户线程:不需要内核支持在用户程序中实现的线程;
Python3 中的多线程:
#!/usr/bin/env python import _thread def print_time( threadName, delay): print (threadName) count = 0 while 1: pass count += 1 try: _thread.start_new_thread( print_time, ("Thread-1", 1, ) ) _thread.start_new_thread( print_time, ("Thread-2", 2, ) ) _thread.start_new_thread( print_time, ("Thread-3", 2, ) ) _thread.start_new_thread( print_time, ("Thread-4", 2, ) ) _thread.start_new_thread( print_time, ("Thread-5", 2, ) ) _thread.start_new_thread( print_time, ("Thread-6", 2, ) ) _thread.start_new_thread( print_time, ("Thread-7", 2, ) ) _thread.start_new_thread( print_time, ("Thread-8", 2, ) ) _thread.start_new_thread( print_time, ("Thread-9", 2, ) ) _thread.start_new_thread( print_time, ("Thread-10", 2, ) ) _thread.start_new_thread( print_time, ("Thread-11", 2, ) ) _thread.start_new_thread( print_time, ("Thread-12", 2, ) ) _thread.start_new_thread( print_time, ("Thread-13", 2, ) ) _thread.start_new_thread( print_time, ("Thread-14", 2, ) ) _thread.start_new_thread( print_time, ("Thread-15", 2, ) ) except: print ("Error: can‘t start thread!") while 1: pass
#!/usr/bin/env python3 import threading import time exitFlag = 0 class myThread (threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print ("start" + self.name) print_time(self.name, self.counter, 5) print ("exit" + self.name) def print_time(threadName, delay, counter): while counter: if exitFlag: threadName.exit() time.sleep(delay) print ("%s: %s" % (threadName, time.ctime(time.time()))) counter -= 1 thread1 = myThread(1, "Thread-1", 1) thread2 = myThread(2, "Thread-2", 2) thread1.start() thread2.start() thread1.join() thread2.join() print ("exit!")
python 的多线程 threading 有时候并不是特别理想. 最主要的原因是就是, Python 的设计上, 有一个必要的环节, 就是 Global Interpreter Lock (GIL). 这个东西让 Python 还是一次性只能处理一个东西:
尽管Python完全支持多线程编程, 但是解释器的C语言实现部分在完全并行执行时并不是线程安全的。 实际上,解释器被一个全局解释器锁保护着,它确保任何时候都只有一个Python线程执行。 GIL最大的问题就是Python的多线程程序并不能利用多核CPU的优势 (比如一个使用了多个线程的计算密集型程序只会在一个单CPU上面运行);
GIL只会影响到那些严重依赖CPU的程序(比如计算型的)。 如果你的程序大部分只会涉及到I/O,比如网络交互,那么使用多线程就很合适, 因为它们大部分时间都在等待;
import threading from queue import Queue import copy import time 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)
保持更新,转载请注明出处,更多内容请关注cnblogs.com/xuyaowen;
参考链接:
https://morvanzhou.github.io/tutorials/python-basic/threading/5-GIL/
原文:https://www.cnblogs.com/xuyaowen/p/Python3-thread-model.html