How can an int be '+' with a tuple?
Jach Fong
jfong at ms4.hinet.net
Sat Jun 2 22:55:04 EDT 2018
The attached is a script which can run under Python 3.4/Windows Vista
correctly. One thing make me puzzled is that the "any + context" at line
18. The "any" was passed as an integer from line 43 and the "context"
was defined as a tuple at line 35. This concatenation works! how?
Best Regards,
Jach Fong
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
import _thread as thread
import queue
threadQueue = queue.Queue(maxsize=0)
def queueChecker(widget, delayMsecs=100):
try:
(callback, args) = threadQueue.get(block=False)
except queue.Empty:
pass
else:
callback(*args)
widget.after(delayMsecs,
lambda: queueChecker(widget, delayMsecs)) # back to event loop
def threaded(action, args, context, onExit, onProgress):
def progress(*any):
threadQueue.put((onProgress, any + context))
action(progress=progress, *args)
threadQueue.put((onExit, context))
def startThread(action, args, context, onExit, onProgress):
thread.start_new_thread(
threaded, (action, args, context, onExit, onProgress))
if __name__ == '__main__':
import time
import tkinter as tk
def onEvent(i): # code that spawns thread
myname = 'thread-%s' % i
startThread(
action = threadaction,
args = (i, 3),
context = (myname,),
onExit = threadexit,
onProgress = threadprogress)
# thread's main action
def threadaction(id, reps, progress):
for i in range(reps):
time.sleep(1)
progress(i) # progress callback: queued
# thread exit/progress callbacks: dispatched off queue in main thread
def threadexit(myname):
print('%s\texit' % myname)
def threadfail(exc_info, myname):
print('%s\tfail\t%s' % (myname, exc_info[0]))
def threadprogress(count, myname):
print('%s\tprog\t%s' % (myname, count))
# make enclosing GUI and start timer loop in main thread
# spawn batch of worker threads on each mouse click: may overlap
root = tk.Tk()
queueChecker(root)
root.bind('<Button-1>', # 3.x need list for map, range ok
lambda event: list(map(onEvent, range(2))) )
root.mainloop()
More information about the Python-list
mailing list