Sunday, July 21, 2024

Multi thread in Python

'''
threading
1. No return
2. Can run seperatr function
concurrent.futures
1. Return
2. Only run 1 Function in 1 map
'''
from concurrent.futures import *
import threading
import time
l=[]
def fn(i):
print(f"{threading.get_native_id()}")
time.sleep(i)
l.append(i)
return i
t1=threading.Thread(target=fn,args=(1,))
t1.start()
t1.join()
with ThreadPoolExecutor(2) as e:
res=e.map(fn,[2,3])
for i in res:print(i)
print(l)
view raw multiThread.py hosted with ❤ by GitHub

No comments:

Post a Comment