python - Is it possible to use an inline function in a Thread call? -
the documentation threading.thread(target=...) states that
targetcallable object invoked run() method. defaults none, meaning nothing called.
i use this:
import threading def worker(): = 3 print("bonjour {a}".format(a=a)) threading.thread(target=worker).start() is there way chain function elements in target new 1 not need defined? (pseudocode obviously)
threading.thread(target=(a=3;print("bonjour {a}".format(a=a))).start() i have bunch of short calls make in thread call , avoid multiplication of function definitions.
you can use lambda function in python 3.x
import threading threading.thread(target=lambda a: print("hello, {}".format(a)), args=(["world"])) you should take @ this question see why can't use print in python 2.x in lambda expressions.
actually, can fit many function calls lambda:
from __future__ import print_function # i'm on python 2.7 threading import thread thread(target=(lambda: print('test') == print('hello'))).start() that print both test , hello.
Comments
Post a Comment