minte9
LearnRemember




S R Q

Threads

p400 A multithreaded program is like having multip many fingers.
 
"""Threads:
When you use time.sleep() your program cannot do anything else,
unless you use threads.
A single-thread program is like placing one finger on a line of code,
then moving to the next.
A multi-threaded program has multiple "fingers"
"""
import threading, time

def pause():
    time.sleep(5)
    print('Wake up! ' + threading.currentThread().name)

print('Program start') # main

A = threading.Thread(target=pause) # second
A.start()
B = threading.Thread(target=pause, name='B') # third
B.start()

print('Program end') # main

# Program start
# Program end
# Wake up! Thread-1
# Wake up! B

Arguments

p402 You can pass target function's arguments.
 
"""Thread function's arguments
When target function you want to call to run in a thread 
has 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')

# Start main
# Run thread: B
# End main
# Run thread: B
# Run thread: B
# Run thread: B
# Run thread: B

Concurrency

To avoid concurrency issues, never let threads change the same variable.
 
"""Threads Concurrency:
When you create a new thread make sure its target function
uses only local variable in that function.
"""
import threading, time


"""Incorrect"""
i = 0
def pause():
    global i # Look Here
    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')
    # Start
    # Run thread: A
    # Run thread: B
    # End
    # Run thread: A

time.sleep(3)
print()


"""Correct"""
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')
    # Start
    # Run thread: A2
    # Run thread: B2
    # End
    # Run thread: A2
    # Run thread: B2
    # Run thread: A2
    # Run thread: B2

HTTP server (A)

Open HTTP server and URL in the default browser.
 
"""
Start server & Open URL

python startweb.py
    # Ctrl+C to kill the process
    # ps -aF | grep python
    # kill -9 no

Alternatives
    php -S localhost:8000
    python3 -m http.server 8000 
    http://localhost:8000/public_html
"""

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)

Questions    
Scheduler, Subprocess