Skip to content

Instantly share code, notes, and snippets.

@mrchoke
Last active February 29, 2020 14:31
Show Gist options
  • Save mrchoke/d94b247fd978696f17aa5f5b2d5c2331 to your computer and use it in GitHub Desktop.
Save mrchoke/d94b247fd978696f17aa5f5b2d5c2331 to your computer and use it in GitHub Desktop.
ตัวอย่างการแตก Thread มารับงานทำเสร็จก็เสร็จกันไปไม่ต้องรอ ง่ายกว่าใช้ Threading แบบปกติ
import concurrent.futures
from datetime import datetime
import time
import random
def process(i, data):
t = random.randrange(5)
time.sleep(t)
print("Process index:", i, ' Sleep:', t, ' Current:', datetime.now())
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
for i in range(10):
executor.submit(process, i, datetime.now())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment