首页 > 编程语言 > 详细

Python 3 线程模型记录

时间:2019-10-20 14:45:12      阅读:65      评论:0      收藏:0      [点我收藏+]

最近需要使用 python3 多线程处理大型数据,顺道探究了一下,python3 的线程模型的情况,下面进行简要记录;

多线程运行的优点:

  •  使用线程可以把程序中占用时间较长的任务放到后台去处理;
  • 用户界面可以更加吸引人,并且不阻塞界面的运行;
  • 程序运行的速度可以更快;
  • 充分利用CPU多核的特征进行处理;

内核线程:由操作系统内核创建和撤销;

用户线程:不需要内核支持在用户程序中实现的线程;

Python3 中的多线程:

  • _thread 提供了一些原始的api 用于写多线程程序;
  • threading 提供了更加便利的接口
  • 两者都是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
_thread测试
技术分享图片
#!/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!")
threading测试

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)
GIL测试

保持更新,转载请注明出处,更多内容请关注cnblogs.com/xuyaowen;

参考链接:

https://morvanzhou.github.io/tutorials/python-basic/threading/5-GIL/ 

https://python3-cookbook.readthedocs.io/zh_CN/latest/c12/p09_dealing_with_gil_stop_worring_about_it.html

Python 3 线程模型记录

原文:https://www.cnblogs.com/xuyaowen/p/Python3-thread-model.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!