Код: Выделить всё
import concurrent.futures
def f2():
print("hello, f2")
def f1():
print("hello, f1")
executor.submit(f2)
with concurrent.futures.ThreadPoolExecutor(16) as executor:
executor.submit(f1)
Код: Выделить всё
hello, f1
Код: Выделить всё
import concurrent.futures
def f2():
print("hello, f2")
return 3
def f1():
print("hello, f1")
print(executor.submit(f2).result())
with concurrent.futures.ThreadPoolExecutor(16) as executor:
executor.submit(f1)
Код: Выделить всё
import concurrent.futures
def f2():
print("hello, f2")
return 3
def f1():
print("hello, f1")
return executor.submit(f2).result()
with concurrent.futures.ThreadPoolExecutor(16) as executor:
print(executor.submit(f1).result())
Код: Выделить всё
hello, f1
hello, f2
3
Обновление:
Без with, вывод примера 1 является случайным (т.е. иногда выводит hello, f2, а иногда нет)
Реальный использование такое:
Код: Выделить всё
import concurrent.futures
def f3(arg1, arg2):
print(f"hello, f3, {arg1}, {arg2}")
def f2(arg1, arg2):
print(f"hello, f2 {arg1}")
for i in range(10):
executor.submit(f3, arg2, i)
def f1(arg1, arg2, arg3):
print(f"hello, f1 {arg1}")
for i in range(10):
executor.submit(f2, i, arg2, arg3)
with concurrent.futures.ThreadPoolExecutor(16) as executor:
for i in range(10):
executor.submit(f1, i, 1, 2)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -submitted