Home  >  Article  >  Backend Development  >  Three ways to get thread return value in Python

Three ways to get thread return value in Python

WBOY
WBOYforward
2023-04-13 10:43:092107browse

When it comes to threads, your brain should have this impression: We can control when it starts, but we cannot control when it ends. So how to get the return value of the thread? Today I will share some of my own practices.

Method 1: Use a list of global variables to save the return value

ret_values = []

def thread_func(*args):
...
value = ...
ret_values.append(value)

One reason for choosing a list is that the append() method of the list is thread-safe. In CPython, the GIL prevents their concurrent access. If you use a custom data structure, you need to add a thread lock where the data is modified concurrently.

If you know how many threads there are in advance, you can define a fixed-length list, and then store the return value according to the index, for example:

from threading import Thread

threads = [None] * 10
results = [None] * 10

def foo(bar, result, index):
result[index] = f"foo-{index}"

for i in range(len(threads)):
threads[i] = Thread(target=foo, args=('world!', results, i))
threads[i].start()

for i in range(len(threads)):
threads[i].join()

print (" ".join(results))

Method 2: Rewrite the join method of Thread and return Return value of thread function

The default thread.join() method just waits for the thread function to end and has no return value. We can return the running result of the function here. The code is as follows:

from threading import Thread


def foo(arg):
return arg


class ThreadWithReturnValue(Thread):
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)

def join(self):
super().join()
return self._return


twrv = ThreadWithReturnValue(target=foo, args=("hello world",))
twrv.start()
print(twrv.join()) # 此处会打印 hello world。

In this way, when we call thread.join() and wait for the thread to end, we will get the return value of the thread.

Method 3: Use the standard library concurrent.futures

I think the first two methods are too low-level. Python’s standard library concurrent.futures provides more advanced thread operations and can directly obtain threads. The return value is quite elegant. The code is as follows:

import concurrent.futures


def foo(bar):
return bar


with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
to_do = []
for i in range(10):# 模拟多个任务
future = executor.submit(foo, f"hello world! {i}")
to_do.append(future)

for future in concurrent.futures.as_completed(to_do):# 并发执行
print(future.result())

The result of a certain operation is as follows:

hello world! 8
hello world! 3
hello world! 5
hello world! 2
hello world! 9
hello world! 7
hello world! 4
hello world! 0
hello world! 1
hello world! 6

The above is the detailed content of Three ways to get thread return value in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Magic methods in PythonNext article:Magic methods in Python