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

python实现线程池的方法

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

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

本文实例讲述了python实现线程池的方法。分享给大家供大家参考。具体如下:

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

文件名:thrd_pool.py 系统环境:ubuntu linux & python2.6

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>

执行方式:python thrd_pool.py

执行结果:

count:0, thrd_name:Thread-1
count:1, thrd_name:Thread-2
count:2, thrd_name:Thread-3
count:3, thrd_name:Thread-4
count:4, thrd_name:Thread-5
count:5, thrd_name:Thread-1
count:6, thrd_name:Thread-6
count:7, thrd_name:Thread-2
count:8, thrd_name:Thread-3
count:9, thrd_name:Thread-4
count:10, thrd_name:Thread-5
count:11, thrd_name:Thread-1
count:12, thrd_name:Thread-6
count:13, thrd_name:Thread-2
count:14, thrd_name:Thread-3
('Oops! Got signal %s', 15)

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

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

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

相关文章

  • python实现TCP服务器端与客户端的方法详解
  • 举例讲解Python中is和id的用法
  • python3.5实现socket通讯示例(TCP)
  • python数据结构之二叉树的统计与转换实例
  • Python基于动态规划算法计算单词距离
  • python下如何让web元素的生成更简单的分析
  • Python命名空间详解
  • python操作摄像头截图实现远程监控的例子
  • 详细解析Python中__init__()方法的高级应用
  • Python处理文本文件中控制字符的方法

文章分类

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

最近更新的内容

    • python检测服务器是否正常
    • Python多线程实现同步的四种方式
    • Django Admin实现上传图片校验功能
    • Python标准库之多进程(multiprocessing包)介绍
    • 实例说明Python中比较运算符的使用
    • 在Python中操作列表之list.extend()方法的使用
    • 在Python的gevent框架下执行异步的Solr查询的教程
    • Django框架中方法的访问和查找
    • Python基于DES算法加密解密实例
    • python数据库操作常用功能使用详解(创建表/插入数据/获取数据)

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

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