|
langPython
Thread basics.
from tkinter import *
from _thread import start_new_thread
from time import sleep
x=0
def weegbrug():
global x
while True:
x=x+1
sleep(0.5)
start_new_thread(weegbrug,())
def display():
v.set(x)
root.after(500, display)
root = Tk()
v = StringVar()
txt = Label(root, textvariable=v, width=800, height=600, bg='yellow', font=('Helvetica', 300))
txt.pack(expand=YES, fill=BOTH)
root.title('Weegbrug')
root.overrideredirect(1)
root.geometry('%dx%d+0+0' % (root.winfo_screenwidth(), root.winfo_screenheight()))
root.after(500, display)
root.mainloop()from subprocess import *
from tkinter import *
from threading import Thread
from queue import Queue
from time import sleep
class Weegbrug(Thread):
def __init__(self, gui):
Thread.__init__(self)
self.gui = gui
self.queue = Queue()
def run(self):
while True:
with open('com1', 'a+') as f:
f.write('\r\n')
for line in f:
self.queue.put(line[2:-1])
self.gui.event_generate('<<LineRead>>')
sleep(0.5)
def get_line(self):
return self.queue.get()
check_call(["mode", "COM1:9600,N,8,1,P"],shell=True)
r = Tk()
r.title('Weegbrug')
r.overrideredirect(1)
r.geometry('%dx%d+0+0' % (r.winfo_screenwidth(),r.winfo_screenheight()))
r.bind('<<LineRead>>', lambda *args: v.set(w.get_line()))
v = StringVar()
v.set('00000')
t = Label(r, textvariable=v, width=100, bg='yellow', font=('Helvetica', 300))
t.pack(expand=YES, fill=BOTH)
w = Weegbrug(r)
w.start()
r.mainloop()import os
import threading
GL = {}
GL['x'] = 0
TL = threading.local()
lock = threading.Lock()
def application(environ, response):
try: TL.x += 1
except AttributeError: TL.x=1
with lock:
GL['x'] += 1
GL[id(threading.current_thread())] = TL.x
output = "Pid = {0}\n".format (os.getpid())
output += "Version = {0}\n".format(environ['mod_wsgi.version'])
output += "Process = {0}\n".format(environ['mod_wsgi.process_group'])
output += "Application = {0}\n".format(environ['mod_wsgi.application_group'])
output += "Global x = {0}.\n".format(GL['x'])
tc = threading.current_thread()
count = 0
for t in threading.enumerate():
output += " {0} {1} {2}\n".format(GL[id(t)], t, "CURRENT" if tc==t else "")
count += GL[id(t)]
output += " {0}".format(count)
response('200 OK', [('Content-type', 'text/plain'),('Content-Length', str(len(output)))])
return [output]
|