Last post discussed parallelism. Threads can be useful for timing stats. In this example, a single thread is spawned for the timing function. Obviously you can spawn purpose specific threads to do whatever you need. Communication with threads is simple because you can just use local variables. Python built in types are thread safe, so it's quite simple, no need to use threading.Lock().
import random
import threading
import json
import time
timingstats={"starttime":0,"processtime":0,"rate":0,"requests":0,"lastreq":0}
exitflag=0
def timingfunction():
a=0
while True:
time.sleep(0.01)
a+=1
if a%500==0 or exitflag==1:
print("## WORD RATE ## : {}".format(timingstats["rate"]))
if exitflag==1: return
def req(n):
global exitflag
entry=""
while entry != n:
entry=input("Type: {} :".format(n))
if entry=="exit":
exitflag=1
exit()
timingstats["requests"]+=1
now=time.time()
timingstats["lastreq"]=now
timingstats["rate"]=float(timingstats["requests"])/(now-timingstats["starttime"])
timingthread=threading.Thread(target=timingfunction)
timingthread.start()
timingstats["starttime"]=time.time()
words="hairy,scary,mary,carbon,chief,hat,blah,lunchbox,cloud,crap"
wordlist=words.split(",")
while True:
chk=random.randint(0,len(wordlist)-1)
req(wordlist[chk])
import threading
import json
import time
timingstats={"starttime":0,"processtime":0,"rate":0,"requests":0,"lastreq":0}
exitflag=0
def timingfunction():
a=0
while True:
time.sleep(0.01)
a+=1
if a%500==0 or exitflag==1:
print("## WORD RATE ## : {}".format(timingstats["rate"]))
if exitflag==1: return
def req(n):
global exitflag
entry=""
while entry != n:
entry=input("Type: {} :".format(n))
if entry=="exit":
exitflag=1
exit()
timingstats["requests"]+=1
now=time.time()
timingstats["lastreq"]=now
timingstats["rate"]=float(timingstats["requests"])/(now-timingstats["starttime"])
timingthread=threading.Thread(target=timingfunction)
timingthread.start()
timingstats["starttime"]=time.time()
words="hairy,scary,mary,carbon,chief,hat,blah,lunchbox,cloud,crap"
wordlist=words.split(",")
while True:
chk=random.randint(0,len(wordlist)-1)
req(wordlist[chk])
sample output:
python3 tst3.py
Type: chief :chief
Type: blah :blah
Type: chief :chief
Type: blah :## WORD RATE ## : 0.6276005416066457
blah
Type: blah :blah
Type: lunchbox :lunchbox
Type: hat :## WORD RATE ## : 0.5847226421265149
hat
Type: blah :blah
Type: crap :crap
Type: lunchbox :## WORD RATE ## : 0.6115188345845941
exit
## WORD RATE ## : 0.6115188345845941
The timing thread is running on it's own, interrupting me mid-word etc. This is useful if you're making a network app and want statistics, checks, monitoring etc. Maybe the output is to a file, or over the network. For terminal apps, it can be used well if you specify text position.
e.g.
print("\033[6;53H###Hello")
will output ###Hello at row 5, column 53 in the terminal (not windows).
e.g. you can use several different checks on different threads, and their output status can write data to different areas of the screen. Alternatively (and generrally better idea for bigger things), different threads for different checks write status to status list variable, and a screen updater thread uses that status to draw the screen.