Threads
A multithreaded program is like having multip
many fingers.

import threading, time
def pause():
time.sleep(5)
print('Wake up! ' + threading.currentThread().name)
print('Program start')
A = threading.Thread(target=pause)
A.start()
B = threading.Thread(target=pause, name='B')
B.start()
print('Program end')
Arguments
You can
pass target function's arguments.

import threading, time
def pause(seconds):
i = 0
while i < seconds:
print('Run thread: ' + threading.currentThread().name)
i = i + 1
time.sleep(1)
print('Start main')
threading.Thread(target=pause, name='B', args=[5]).start()
print('End main')
Concurrency
To avoid
concurrency issues, never let threads change the same variable.

import threading, time
i = 0
def pause():
global i
while i < 3:
print('Run thread: ' + threading.currentThread().name)
i = i + 1
time.sleep(1)
print('Start')
threading.Thread(target=pause, name='A').start()
threading.Thread(target=pause, name='B').start()
print('End')
time.sleep(3)
print()
def pauseC():
i = 0
while i < 3:
print('Run thread: ' + threading.currentThread().name)
i = i + 1
time.sleep(1)
print('Start')
threading.Thread(target=pauseC, name='A2').start()
threading.Thread(target=pauseC, name='B2').start()
print('End')
HTTP server (A)
Open
HTTP server and URL in the default browser.

import os, pathlib, sys, time, threading, webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
DIR = pathlib.Path(__file__).resolve().parent
os.chdir(DIR)
def start_server():
httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
threading.Thread(target=start_server).start()
webbrowser.open_new('http://127.0.0.1:8000/')
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
sys.exit(0)