• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现线程池代码分享

Python实现线程池代码分享

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python 线程池,python线程池原理,python3 线程池,python 多线程,python多线程编程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码

import threading
import time
import signal
import os
 
class task_info(object):
  def __init__(self):
    self.func = None
    self.parm0 = None
    self.parm1 = None
    self.parm2 = None
   
class task_list(object):
  def __init__(self):
    self.tl = []
    self.mutex = threading.Lock()
    self.sem = threading.Semaphore(0)
   
  def append(self, ti):
    self.mutex.acquire()
    self.tl.append(ti)
    self.mutex.release()
    self.sem.release()
   
  def fetch(self):
    self.sem.acquire()
    self.mutex.acquire()
    ti = self.tl.pop(0)    
    self.mutex.release()
    return ti
   
class thrd(threading.Thread):
  def __init__(self, tl):
    threading.Thread.__init__(self)
    self.tl = tl
   
  def run(self):
    while True:
      tsk = self.tl.fetch()
      tsk.func(tsk.parm0, tsk.parm1, tsk.parm2)  
 
class thrd_pool(object):
  def __init__(self, thd_count, tl):
    self.thds = []
     
    for i in range(thd_count):
      self.thds.append(thrd(tl))
   
  def run(self):
    for thd in self.thds:
      thd.start()
       
       
def func(parm0=None, parm1=None, parm2=None):
  print 'count:%s, thrd_name:%s'%(str(parm0), threading.currentThread().getName())
   
def cleanup(signo, stkframe):
  print ('Oops! Got signal %s', signo) 
   
  os._exit(0)
   
if __name__ == '__main__':
   
  signal.signal(signal.SIGINT, cleanup)
  signal.signal(signal.SIGQUIT, cleanup)
  signal.signal(signal.SIGTERM, cleanup)
   
  tl = task_list()
  tp = thrd_pool(6, tl)
  tp.run()
   
  count = 0
  while True:
     
    ti = task_info()
    ti.parm0 = count
    ti.func = func
    tl.append(ti)
    count += 1
     
    time.sleep(2)
  pass
</div>

</div>

您可能想查找下面的文章:

  • 不要用强制方法杀掉python线程
  • Python线程指南详细介绍
  • Python线程指南详细介绍
  • python 线程的暂停, 恢复, 退出详解及实例
  • python实现线程池的方法
  • Python线程详解
  • Python实现线程池代码分享
  • python使用装饰器和线程限制函数执行时间的方法
  • Python线程的两种编程方式
  • 用Python实现一个简单的线程池

相关文章

  • Python只用40行代码编写的计算器实例
  • python 随机数生成的代码的详细分析
  • Python打印斐波拉契数列实例
  • Python作用域用法实例详解
  • Python多线程编程(二):启动线程的两种方法
  • 使用python实现正则匹配检索远端FTP目录下的文件
  • Pyhthon中使用compileall模块编译源文件为pyc文件
  • 详解Python中__str__和__repr__方法的区别
  • python根据距离和时长计算配速示例
  • python获取本机外网ip的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python使用爬虫猜密码
    • python base64 decode incorrect padding错误解决方法
    • Python 递归函数详解及实例
    • python压缩文件夹内所有文件为zip文件的方法
    • 使用Python的Django框架实现事务交易管理的教程
    • 浅析Python中else语句块的使用技巧
    • python实现根据主机名字获得所有ip地址的方法
    • Python Django使用forms来实现评论功能
    • 一篇不错的Python入门教程
    • Python比较两个图片相似度的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有