A-A+

python回调函数中使用多线程

2019年11月21日 文章转载 暂无评论 阅读 2,277 views 次

python回调函数demo

下面的demo是根据需求写的简单测试脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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:

1
2
3
4
5
6
7
8
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的多线程

下面的代码是从主程序中,摘取出来的代码片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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上老外给的代码示例:

1
2
3
4
5
6
7
8
9
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

1
[3, 5, 7]

Stack Overflow上更多的答疑方便你更好的理解:
https://stackoverflow.com/questions/5442910/python-multiprocessing-pool-map-for-multiple-arguments

相信聪明的你一定能看得懂~

多线程与多进程

1
from multiprocessing.dummy import Pool as ThreadPool

多线程进程池,绑定一个CPU核心

1
from multiprocessing import Pool

多进程,运行于多个cpu核心

如果你搞不懂是CPU密集型的任务,还是IO密集型的任务,那就用这个库两条import都写上,然后分别实例化跑一下就知道耗时长短,用法上只是在创建对象上改几个字母就行Pool和ThreadPool的互换。

老外实例妙趣横生的讲解:《一行代码搞并行》
http://chriskiehl.com/article/parallelism-in-one-line/

学习记录贴,fighting~

 

原文链接:http://pirogue.org/2017/12/23/call_back_func/

标签:

给我留言