multiprocessing.pool使用多参数

更新时间:2023-07-11 03:55:43 阅读: 评论:0

multiprocessing.pool使⽤多参数0. 多参数理解
*add 的使⽤⽅法
不加*会出现:TypeError: action() takes 1 positional argument but 3 were given
import threading
#定义线程要调⽤的⽅法,*add可接收多个以⾮关键字⽅式传⼊的参数
def action(*add):
for arc in add:
#调⽤ getName() ⽅法获取当前执⾏该程序的线程名
行笔print(threading.current_thread().getName() +" "+ arc)
#定义为线程⽅法传⼊的参数
my_tuple = ("c.biancheng/python/",\
"c.biancheng/shell/",\
"c.biancheng/java/")
#创建线程
安卓加密thread = threading.Thread(target = action,args =my_tuple)
#启动线程
thread.start()
#指定 thread 线程优先执⾏完毕
thread.join()
#主线程执⾏如下语句
for i in range(5):
print(threading.current_thread().getName())
1. map ,map_async
# f 参数是两个,multiprocessing.pool.map框架只能传⼀个的时候
from multiprocessing import Pool
import time
# 1 这个⽅法不⾏,但是装饰器思路好
# def my_function_helper(func):
#    def inner(tup):
#        print('start wrapper is {}'.format(tup))
#        r = func(*tup)
#        print('end wrapper')
#        return r
#    return inner
#
#没头脑和不高兴读后感
# @my_function_helper
# def f(x,y):
#    print (x*x)
#    print(y)
# 2 这个⽅法可⾏,不⽤改变原函数
def f(x,y):
print (x*x,y)
def my_function_helper(tup):
return f(*tup)
if __name__ == '__main__':
pool = Pool(process=4)
# ⼀个参数的情况
# pool.map(f, [i for i in range(10)])
# r = pool.map_async(f, [i for i in range(10)])
# 两个参数的情况
pool.map(my_function_helper, [(i,2) for i in range(10)])
r = pool.map_async(my_function_helper, [(i,2) for i in range(10)])菊花分类
# DO STUFF
print ('HERE')
print ('MORE')
r.wait()
print ('DONE')
2. starmap (类似map是同步的,看例⼦)
from multiprocessing import Pool, cpu_count
import time
build_links = [1,2,3,4,5,6,7,8]
电脑屏幕闪烁不停怎么回事auth = 'auth'
def test(url, auth):
time.sleep(2)
with open('1.txt',mode='w') as f:
f.write('test')
print(url, auth)
def test2(url, auth):
time.sleep(6)
with open('2.txt',mode='w') as f:
f.write('test2')
print(url, auth)
if __name__ == '__main__':  # 必须要加,不然出错,以避免递归创建⼦流程。    with Pool(process=int(cpu_count() - 1) or 1) as pool:
pool.starmap(test, [(link, auth) for link in build_links])
print('要等上⾯的函数执⾏完成!')
pool.starmap(test2, [(link, auth) for link in build_links])
豌豆苗怎么做好吃
3. 多参数异步的实现
from multiprocessing import Pool, cpu_count
import time
build_links = [1,2,3,4,5,6,7,8]
auth = 'auth'
def test(url, auth):
time.sleep(2)
with open('1.txt',mode='w') as f:
f.write('test')
print(url, auth)
高中励志视频def test2(url, auth):
time.sleep(6)
with open('2.txt',mode='w') as f:
f.write('test2')
print(url, auth)
def my_test_helper(tup):
return test(*tup)
def my_test2_helper(tup):
return test2(*tup)
if __name__ == '__main__':  # 必须要加,不然出错,以避免递归创建⼦流程。    with Pool(process=int(cpu_count() - 1) or 1) as pool:
print('1')
r = pool.map_async(my_test_helper, [(link, auth) for link in build_links])        r2 = pool.map_async(my_test2_helper, [(link, auth) for link in build_links])        print('2')
r.wait()
print('MORE')
r2.wait()
print('DONE')
韩国用英语怎么说

本文发布于:2023-07-11 03:55:43,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1076556.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:线程   参数   避免
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图