Created
July 21, 2024 16:22
-
-
Save j-thepac/47092f7c84f3b0eb300cc853b53f92f6 to your computer and use it in GitHub Desktop.
MultiThread in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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