python - linux - starting a thread immediately -
here's example code
while true: #main-loop if command_received: thread = thread(target = doitnow) thread.start() ...... def doitnow(): some_blocking_operations()
my problem need "some_blocking_operations" start (as command_received true). since they're blocking can't execute them on main loop , can't change "some_blocking_operations" non-blocking either
for "immediately" mean possible, not more 10ms delay. (i once got whole second of delay). if it's not possible, constant delay acceptable. (but must constant. few milliseconds of error)
i'm working on linux system (ubuntu, may 1 in future. linux)
a python solution amazing.. different 1 better nothing
any ideas? in advance
from threading import thread class worker(thread): def __init__(self, someparameter=true): thread.__init__(self) # how "send" parameters/variables # thread on start-up thread can use. # example in case need it. self.somevariable = someparameter self.start() # note: makes thread self-starting, # call .start() in main loop. def run(): # , how use variable/parameter # passed on when created thread. if self.somevariable true: some_blocking_operations() while true: #main-loop if command_received: worker()
this non-blocking execution of some_blocking_operations()
in threaded manner. i'm not sure if you're looking wait thread finish or not, or if care?
if want wait "command" received , execute blocking operations without waiting it, verifying completes, should work you.
python mechanics of threading
python run in 1 cpu core, you're doing here running multiple executions on overlapping clock invervals in cpu. meaning every other cycle in cpu execution made in main thread, , other blocking call chance run execution. won't run in parallel.
there are "you can, but..." threads.. one:
Comments
Post a Comment