[英]Passing arguments to a function from list using Threads in python
I am trying to pass arguments to a function from a list using threads in python (Note:The values to the function should not be hard-coded and all the elements of the list will be passed.) Please look at the sample code here: 我正在尝试使用python中的线程将参数从列表传递给函数(注意:该函数的值不应经过硬编码,并且列表的所有元素都将被传递。)请在此处查看示例代码:
from threading import Thread
list=['john','doe','srav','dev','app']
def m1(name,prefix):
for ele in range(2):
print(name +prefix)
def main1():
t1=Thread(target=m1,args=('abcd','john'))
t2 = Thread(target=m1, args=('abcd', 'doe'))
t1.start()
t2.start()
if __name__ == '__main__':
main1()
Here i hard-coded the values to the function ('john','doe')instead of that pass from the list and all the elements will be passed. 在这里,我将值硬编码到函数(“ john”,“ doe”),而不是列表中的传递,所有元素都将传递。
I am not sure if I completely understand what you are looking to achieve. 我不确定我是否完全了解您要实现的目标。 My understanding is that you want to start a separate thread for each value in your list, so each value gets processed "in parallel". 我的理解是,您想为列表中的每个值启动一个单独的线程,因此每个值都可以“并行”处理。 I modified your main1
function to do so: 我修改了main1
函数来做到这一点:
def main1():
threads = [Thread(target=m1,args=('abcd',x)) for x in list]
for thread in threads: thread.start()
for thread in threads: thread.join()
threads = [Thread(target=m1,args=('abcd',x)) for x in list]
creates a separate thread for each value in the list. threads = [Thread(target=m1,args=('abcd',x)) for x in list]
为列表中的每个值创建一个单独的线程。
for thread in threads: thread.start()
starts each thread. for thread in threads: thread.start()
启动每个线程。
for thread in threads: thread.join()
makes sure each thread is finished before returning from the main1
function (if you would rathee return immediately, just delete this line). for thread in threads: thread.join()
确保每个线程在从main1
函数返回之前main1
完成(如果要立即返回,只需删除此行)。
Your tasks are well suited for use with the concurrent.futures
module. 您的任务非常适合与concurrent.futures
模块一起使用。 In particular, Executor.map
applies a function to the elements of an iterable. 特别是, Executor.map
将函数应用于可迭代对象的元素。
You can use something similar to the ThreadPoolExecutor
example in the docs : 您可以使用类似于docs中 ThreadPoolExecutor
示例的内容:
from concurrent.futures import ThreadPoolExecutor
from itertools import repeat
names = ['john', 'doe', 'srav', 'dev', 'app']
def m1(name, prefix):
for _ in range(2):
print(name + prefix)
with ThreadPoolExecutor(2) as executor:
executor.map(m1, repeat('abcd', len(names)), names)
If you find the repeat
syntax to be awkward, you have a couple of alternatives: 如果发现repeat
语法很尴尬,则有两种选择:
with ThreadPoolExecutor(2) as executor:
for name in names:
executor.submit(m1, 'abcd', name)
OR 要么
with ThreadPoolExecutor(2) as executor:
executor.map(lambda name: m1('abcd', name), names)
In all cases, the with
block will implicitly call executor.shutdown
, which will wait for all the tasks to complete. 在所有情况下, with
块都会隐式调用executor.shutdown
,它将等待所有任务完成。
As a rule, don't call a variable list
: it will shadow the name of the builtin class. 通常,不要调用变量list
:它将隐藏内置类的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.