gpt4 book ai didi

python - 线程提示是串行运行,而不是并行运行?

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:39 25 4
gpt4 key购买 nike

我使用线程进行远程 API 调用,不使用连接,以便程序可以进行下一个 API 调用,而无需等待最后一个 API 调用完成。

像这样:

def run_single_thread_no_join(function, args):
thread = Thread(target=function, args=(args,))
thread.start()
return

问题是我需要知道所有 API 调用何时完成。因此,我转向使用提示和连接的代码。

线程现在似乎串行运行。

我似乎不知道如何让连接工作以便线程并行执行。

我做错了什么?

def run_que_block(methods_list, num_worker_threads=10):
'''
Runs methods on threads. Stores method returns in a list. Then outputs that list
after all methods in the list have been completed.

:param methods_list: example ((method name, args), (method_2, args), (method_3, args)
:param num_worker_threads: The number of threads to use in the block.
:return: The full list of returns from each method.
'''

method_returns = []

# log = StandardLogger(logger_name='run_que_block')

# lock to serialize console output
lock = threading.Lock()

def _output(item):
# Make sure the whole print completes or threads can mix up output in one line.
with lock:
if item:
print(item)
msg = threading.current_thread().name, item
# log.log_debug(msg)

return

# The worker thread pulls an item from the queue and processes it
def _worker():

while True:
item = q.get()
if item is None:
break

method_returns.append(item)
_output(item)

q.task_done()

# Create the queue and thread pool.
q = Queue()

threads = []
# starts worker threads.
for i in range(num_worker_threads):
t = threading.Thread(target=_worker)
t.daemon = True # thread dies when main thread (only non-daemon thread) exits.
t.start()
threads.append(t)

for method in methods_list:
q.put(method[0](*method[1]))

# block until all tasks are done
q.join()

# stop workers
for i in range(num_worker_threads):
q.put(None)
for t in threads:
t.join()

return method_returns

最佳答案

您正在主线程中完成所有工作:

for method in methods_list:
q.put(method[0](*method[1]))

假设methods_list中的每个条目都是可调用的及其参数序列,您在主线程中完成了所有工作,然后将每个函数调用的结果放入队列中,这并不除了打印之外,不允许任何并行化(这通常不足以证明线程/队列开销的合理性)。

大概,您希望线程为每个函数完成工作,因此将该循环更改为:

for method in methods_list:
q.put(method) # Don't call it, queue it to be called in worker

并更改_worker函数,以便它调用在线程中执行工作的函数:

def _worker():
while True:
method, args = q.get() # Extract and unpack callable and arguments
item = method(*args) # Call callable with provided args and store result
if item is None:
break

method_returns.append(item)
_output(item)

q.task_done()

关于python - 线程提示是串行运行,而不是并行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41132848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com