Skip to content

Instantly share code, notes, and snippets.

@j-thepac
Created July 21, 2024 16:22
Show Gist options
  • Save j-thepac/47092f7c84f3b0eb300cc853b53f92f6 to your computer and use it in GitHub Desktop.
Save j-thepac/47092f7c84f3b0eb300cc853b53f92f6 to your computer and use it in GitHub Desktop.
MultiThread 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment