Last active
August 13, 2018 14:07
-
-
Save leotada/1e9bde7faf444004140243f3d322fc8d to your computer and use it in GitHub Desktop.
Python Threading and Multiprocessing minimal examples (Single and multiple cores)
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
import time | |
# Method | |
def hello(name, t): | |
time.sleep(t) | |
print(f'Hello {name}') | |
# Parallel thread using a single processor core (use the same main core) | |
import threading | |
t = threading.Thread(target=hello, args=('Leo', 3)) | |
t.start() | |
t.join() | |
# Parallel thread using a other than main processor core (for CPU intensive) | |
from multiprocessing import Process | |
p = Process(target=hello, args=('Leo', 5)) | |
p.start() | |
p.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment