Skip to content

Instantly share code, notes, and snippets.

@leotada
Last active August 13, 2018 14:07
Show Gist options
  • Save leotada/1e9bde7faf444004140243f3d322fc8d to your computer and use it in GitHub Desktop.
Save leotada/1e9bde7faf444004140243f3d322fc8d to your computer and use it in GitHub Desktop.
Python Threading and Multiprocessing minimal examples (Single and multiple cores)
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