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

Three ways to get thread return value in Python

WBOY
Release: 2023-04-13 10:43:09
forward
2327 people have browsed it

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

1

2

3

4

5

6

ret_values = []

 

def thread_func(*args):

...

value = ...

ret_values.append(value)

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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))

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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。

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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())

Copy after login

The result of a certain operation is as follows:

1

2

3

4

5

6

7

8

9

10

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

Copy after login

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template