python回调函数中使用多线程的方法
作者:pirogue 发布时间:2022-11-08 20:01:15
标签:python,回调函数,多线程
下面的demo是根据需求写的简单测试脚本
#!/usr/bin/env python
# coding: utf-8
# 第一个列表为依赖组件和版本号,后面紧跟负责人名称
# 接着出现第二个以来组建列表,负责人为空了
# 所以根据需求需要对组件、版本号、负责人进行不同处理
# 这时在for循环中根据if判断,写回调函数处理
# 格式不一致数据的测试数据
a = [[u'tool-1', u'1.9.13'], u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23'], [u'tool-3', u'1.9.33'], [u'tool-4', u'1.9.43'], u'pi',[u'tool-5', u'1.9.53']]
# a = [[u'tool-1', u'1.9.13'],u'xiaowang',[u'tool-2', u'1.9.23'],u'xiaowang', [u'tool-3', u'1.9.33'],u'xiaowang']
# a = [[u'tool-1', u'1.9.13']]
# [u'tool-1', u'1.9.13']
your_pro = a[0]
# print your_pro
# [u'xiaowang', u'xiaoqu', [u'tool-2', u'1.9.23']]
tmp = a[1:]
# print tmp
def git_callback(whole_v, proj_value, name_value):
# 如果存在负责人存在
try:
if type(name_value[0]) is unicode:
# 对除去列表0个索引的数据(依赖名和版本号)后面的数据进行遍历
for i in name_value:
# 碰到后面的数据是列表的进行回调
if type(i) is list:
tmp_index = whole_v.index(i)+1
return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:])
else:
# 打印依赖、版本号 负责人 开始
print proj_value+i.split()+['start']
else:
# 如果负责人后跟的组件这种格式的列表数据为空
# 也就是只有依赖和版本号列表数据,负责人为空,就打印依赖版本号
ver = proj_value
owner = name_value
if type(owner[0]) is unicode:
return git_callback(whole_v, ver, owner)
else:
print ver
# 这里是为了判断是不是到列表的最后一位
# 如果是最后一个值,且不是字符串的Unicode,而是列表
# 就直接打印出项目
if whole_v.index(owner[0]) == len(whole_v)-1:
# 打印最后一个值
print whole_v[-1:]
else:
# 这里比较绕,打印调试吧...
new_ver = whole_v[whole_v.index(ver)+1]
owner = whole_v[whole_v.index(ver)+2:]
return git_callback(whole_v, new_ver, owner)
except IndexError as e:
print proj_value
print e
git_callback(a, your_pro, tmp)
demo的output:
Boom:git_response pirogue$ python test.py
[u'tool-1', u'1.9.13', u'xiaowang', 'start']
[u'tool-1', u'1.9.13', u'xiaoqu', 'start']
[u'tool-2', u'1.9.23']
[u'tool-3', u'1.9.33']
[u'tool-4', u'1.9.43', u'pi', 'start']
[u'tool-5', u'1.9.53']
list index out of range
python的多线程
下面的代码是从主程序中,摘取出来的代码片段
from multiprocessing.dummy import Pool as ThreadPool
# 判断git查询返回的依赖数据格式不唯一的回调
def git_callback(whole_v, proj_value, name_value, git_cookie):
#
whole_v = whole_v
list_git = []
if name_value:
# print name_value
for i in name_value:
# print i
if i:
if type(i) is list:
tmp_index = whole_v.index(i)+1
return git_callback(whole_v, whole_v[whole_v.index(i)], whole_v[tmp_index:], git_cookie)
else:
git_cookie = str(git_cookie.split()[0])+' '+str(git_cookie.split()[1])
list_git.append(tuple(git_cookie.split("?")+i.split()))
print list_git
pool = ThreadPool(100)
result = pool.map(pool_git, list_git)
print result
pool.close()
pool.join()
else:
print proj_value
上面的多线程代码片段是一个回调函数,没有完全根据demo进行改装,有了demo根据需求改起来也不难,多调试就可以了。
python多线程接收多个参数
from multiprocessing.dummy import Pool as ThreadPool
pool = ThreadPool(100)
result = pool.map(pool_git, list_git)
print result
pool.close()
pool.join()
pool_git是你需要多线程调用的功能函数,list_git是pool_git函数需要接收的参数,默认情况下pool_git是一个接收一个参数的函数。
但是我们的功能常常设计的逻辑比较复杂,需要在pool_git中传入多个参数,这时list_git就应该给一个多个元组组成的列表。
stackoverflow上老外给的代码示例:
def multi_run_wrapper(args):
return add(*args)
def add(x,y):
return x+y
if __name__ == "__main__":
from multiprocessing import Pool
pool = Pool(4)
results = pool.map(multi_run_wrapper,[(1,2),(2,3),(3,4)])
print results
output
[3, 5, 7]
Stack Overflow上更多的答疑方便你更好的理解:
https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments
相信聪明的你一定能看得懂~
多线程与多进程
from multiprocessing.dummy import Pool as ThreadPool
多线程进程池,绑定一个CPU核心
from multiprocessing import Pool
多进程,运行于多个cpu核心
如果你搞不懂是CPU密集型的任务,还是IO密集型的任务,那就用这个库两条import都写上,然后分别实例化跑一下就知道耗时长短,用法上只是在创建对象上改几个字母就行Pool和ThreadPool的互换。
总结
以上所述是小编给大家介绍的python回调函数中使用多线程的方法网站的支持!
来源:http://pirogue.org/2017/12/23/call_back_func/?utm_source=tuicool&utm_medium=referral


猜你喜欢
- 本文实例讲述了wxPython的事件驱动机制,分享给大家供大家参考。具体方法如下:先来看看如下代码:#!/usr/bin/python #
- 看到论坛上有人模仿alert,自己也写了一个。本来想模仿winapi里的MessageBox ;但可惜js 不支持,阻塞模式。返回值只能用异
- 一、简化前馈网络LeNetimport torch as tclass LeNet(t.nn.Module): def __init__(s
- 前言今天在使用 8.0.12 版的 mysql 驱动时遇到了各种各样的坑,在使用 JDBC 连接上遇到的问题可以参考我的上一篇博客。我在使用
- PDO常用方法:PDO::query()主要用于有记录结果返回的操作(PDOStatement),特别是select操作。PDO::exec
- Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数。换言之,就是能使用该模块对可调用对象
- Create trigger tri_wk_CSVHead_History on wk_CSVHead_History --声明一个tri_
- 本文实例为大家分享了javascript canvas实现雨滴效果的具体代码,供大家参考,具体内容如下先看效果看起来很炫酷,其实就是实现了雨
- 1. ASP与Access数据库连接: 代码如下:dim strConn dim conn strConn = "Provide
- GO实现内存数据库实现Redis的database层(核心层:处理命令并返回)https://github.com/csgopher/go-
- 测试环境:先让我们熟悉下基本的sql语句,来查看下我们将要测试表的基本信息use infomation_schemaSELECT * FRO
- 如何制作关联的下拉菜单?看看代码:<form name=f1 METHOD="POST">
- 一、问题Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结。二、Python调用C/C++1、Python
- vue组件在prop里根据type决定传值还是传引用。简要如下:传值:String、Number、Boolean传引用:Array、Obje
- 阅读上一篇:javascript 45种缓动效果(一)这部分对原先的缓动函数进行抽象化,并结合缓动公式进行强化。成品的效果非常惊人逆天。走过
- :is 动态组件使用 v-bind:is=”组件名”,会自动去找匹配的组件名,如果没有,则不显示;<div id="app&
- 前言对于前端项目而言,ESLint 可以检查代码,统一代码风格,避免不必要的错误。在 vue3 中配置 ESLint,如下所示。环境vite
- 如何显示一个文本文件?完整显示文本文件的代码如下: Write(STRING) WriteLine(STRING) WriteBlan
- 在深度学习的数据训练过程中,虽然tensorflow和pytorch都会自带打乱数据进行训练的方法,但是当我们自己生成数据,或者某些情况下依
- 1. __init__ 初始化文件路径,关键字1,关键字2;2. key_match 使用with open 方法,以二进制方式(也可以改成