Python
/
Scheduler
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Scheduler Multithreading
Multithreaded program, like having many fingers Never let threads change the same variable print('start') #main-thread A = threading.Thread(target=myfunc) #second A.start()
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