multithreading - return values with thread.start() in python (using queue) -



multithreading - return values with thread.start() in python (using queue) -

i'd create multi-threaded version of function. find t.start() returns none, have utilize queue. searched documentation, don't understand how utilize in example.

this function:

def derivative(lst, var): # illustration of lst = [1 + [3 * x]] if len(lst) == 1: homecoming derive_solver(lst[0], var) if lst[1] == '+': homecoming [derivative(lst[0], var), '+', derivative(lst[2], var)] if lst[1] == '*': homecoming [[derivative(lst[0], var), '*', lst[2]], '+', [lst[0], '*', derivative(lst[2], var)]]

and effort multi-thread function:

def derivative(lst, var): # illustration of lst = [1 + [3 * x]] if len(lst) == 1: homecoming derive_solver(lst[0], var) if lst[1] == '+': t1 = threading.thread(target = derivative, args=(lst[0], var)) t2 = threading.thread(target = derivative, args=(lst[2], var)) homecoming [t1.start(), '+', t2.start()] if lst[1] == '*': t1 = threading.thread(target = derivative, args=(lst[0], var)) t2 = threading.thread(target = derivative, args=(lst[2], var)) homecoming [[t1.start(), '*', lst[2]], '+', [lst[0], '*', t2.start()]]

the problem t1.start() doesn't homecoming values...

have thought how solve using queue?

thank you!

the problem t1.start() doesn't homecoming values...

of course of study not. t1 hasn't finished @ point. if start waited background thread finish, there absolutely no reason utilize threads in first place.

you need set things background threads post work somewhere , signal they're done, wait until both threads have signaled you. queue 1 way that. shared variable plus condition. or, in case, shared variable plus joining thread. i'll show 1 way queue, since that's asked for:

def enthread(target, args): q = queue.queue() def wrapper(): q.put(target(*args)) t = threading.thread(target=wrapper) t.start() homecoming q q1 = enthread(target = derivative, args=(lst[0], var)) q2 = enthread(target = derivative, args=(lst[2], var)) homecoming [q1.get(), '+', q2.get()]

what did there create queue, pass target function background thread (which wraps real target function), , have background thread set result on queue. then, main thread can wait on queue.

note isn't joining each thread, can problem. can see how expand on code create more robust.

also note we're explicitly waiting thread 1 finish before checking on thread 2. in situation can't until have results anyway, that's fine. in many applications, you'll want single queue, can pick results come in (tagging values in way if need able reconstruct original order).

a much improve solution utilize higher-level abstraction, thread pool or future (or executor, combines both abstractions one). it's worth understanding how these pieces work first, learning how things easy way. so, 1 time understand why works, go read docs on concurrent.futures.

finally, assuming you're using cpython or gil-based implementation—which are—and derive_solver function isn't c extension function explicitly designed of work without gil, isn't going thought in first place. threads great when need concurrency without parallelism (because code simpler way, or because it's i/o bound), when you're trying benefit multiple cores, aren't answer, because 1 thread can run interpreter @ time. utilize multiprocessing (or concurrent.futures.processpoolexecutor instead of concurrent.futures.threadpoolexecutor) if need parallelism.

python multithreading queue

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -