如何将参数传递给线程?

10 浏览
0 Comments

如何将参数传递给线程?

I want to add 5 for every element in range(1,100) with threading module,

to watch which rusult is in which thread.

I finished almost of the code,but how to pass argument into threading.Thread?

import threading,queue
x=range(1,100)
y=queue.Queue()
for i in x:
    y.put(i)
def myadd(x):
    print(x+5)
for i in range(5):
    print(threading.Thread.getName())
    threading.Thread(target=myadd,args=x).start() #it is wrong here
    y.join()

感谢dano的帮助,现在已经没问题了,为了交互式运行,我重新编写了代码:

方法1:交互式运行。

from concurrent.futures import ThreadPoolExecutor
import threading
x = range(1, 100)
def myadd(x):
    print("Current thread: {}. Result: {}.".format(threading.current_thread(), x+5))
def run():
    t = ThreadPoolExecutor(max_workers=5)
    t.map(myadd, x)
    t.shutdown()
run()

方法2:

from concurrent.futures import ThreadPoolExecutor
import threading
x = range(1, 100)
def myadd(x):
    print("Current thread: {}. Result: {}.".format(threading.current_thread(), x+5))
def run():
    t = ThreadPoolExecutor(max_workers=5)
    t.map(myadd, x)
    t.shutdown()
if __name__=="__main__":
    run()

如果要传递更多参数给ThreadPoolExecutor怎么办?

我想用多进程模块计算1 + 3,2 + 4,3 + 45,直到100 + 102。

那20 + 1,20 + 2,20 + 3直到20 + 100怎么办?

from multiprocessing.pool import ThreadPool
do = ThreadPool(5)
def myadd(x,y):
    print(x+y)
do.apply(myadd,range(3,102),range(1,100))

如何修复它?

admin 更改状态以发布 2023年5月21日
0
0 Comments

看起来你正在尝试手动创建线程池,以便使用五个线程将所有100个结果相加。如果是这种情况,我建议使用multiprocessing.pool.ThreadPool

from multiprocessing.pool import ThreadPool
import threading
import queue
x = range(1, 100)
def myadd(x):
    print("Current thread: {}. Result: {}.".format(
               threading.current_thread(), x+5))
t = ThreadPool(5)
t.map(myadd, x)
t.close()
t.join()

如果你使用Python 3.x,你可以使用concurrent.futures.ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor
import threading
x = range(1, 100)
def myadd(x):
    print("Current thread: {}. Result: {}.".format(threading.current_thread(), x+5))
t = ThreadPoolExecutor(max_workers=5)
t.map(myadd, x)
t.shutdown()

我认为你的原始代码有两个问题。首先,你需要将一个元组传递给args关键字参数,而不是一个单一的元素:

threading.Thread(target=myadd,args=(x,))

但是,你还试图将由range(1,100)返回的整个列表(或range对象,如果使用Python 3.x)传递给myadd,这并不是你想要做的。也不清楚你想用队列做什么。也许你想将它传递给myadd

最后要注意的是,Python使用全局解释器锁(GIL),这阻止了多个线程同时使用CPU。这意味着在Python中使用线程进行CPU密集型操作(例如加法)不会提供性能提升,因为只有一个线程会运行。因此,在Python中,更喜欢使用多个进程来并行化CPU密集型操作。你可以通过将第一个示例中的ThreadPool替换为from mulitprocessing import Pool来使上述代码使用多个进程。在第二个示例中,你将使用ProcessPoolExecutor而不是ThreadPoolExecutor。你也可能想要用os.getpid()替换threading.current_thread()

编辑:

以下是处理有两个不同参数传递的情况的方法:

from multiprocessing.pool import ThreadPool
def myadd(x,y):
    print(x+y)
def do_myadd(x_and_y):
    return myadd(*x_and_y)
do = ThreadPool(5)    
do.map(do_myadd, zip(range(3, 102), range(1, 100)))

我们使用zip创建一个列表,其中每个变量在范围内都成对:

[(3, 1), (4, 2), (5, 3), ...]

我们使用map调用每个元组中的do_myadddo_myadd使用元组扩展(*x_and_y)将元组扩展为两个单独的参数,这些参数将传递给myadd

0
0 Comments

在这里,你需要传递一个元组而不是使用单个元素。

代码将是制作元组的。

dRecieved = connFile.readline();
processThread = threading.Thread(target=processLine, args=(dRecieved,)); 
processThread.start();

请参考此处了解更多解释。

0