upgrading from threading to multiprocessing #5
Labels
apprentice
this opportunity is for those looking for a challenge
bug
Something isn't working
enhancement
New feature or request
help wanted
Extra attention is needed
master wizard
this opportunity is not for beginners
all the following code references assume
myThread = threading.Thread(target=func_pointer)
wherefunc_pointer
is the function that is executed by themyThread
object oncemyThread.start()
has been called. NOTE:func_pointer()
is the functionality we want to reuse to smooth acceleration/deceleration on the drivetrain/motor object(s).As it turns out,
myThread.join()
will block until the thread is considered "not alive" which can be determined as abool
by callingmyThread.is_alive()
. If we pass a timeout value tojoin(timeout=5.5)
, then the function will only wait for themyThread
has finished for a up to 5.5 seconds. This does not mean the thread is finished if the timeout has been reached, rather the thread is still busy and join gave up on waiting for it to finish. Despite wheather the thread is alive or not, to execute a threadedfunc_pointer()
, thefunc_pointer
needs to be passed again as thetarget
parameter tonewThread = threading.Thread(target=func_pointer)
object (and initiated withnewThread.start()
function). This is problematic since both threads can be executing simultaneously instead of aborting previous thread and re-start()-ing in under thenewThread
's context. Using thethreading
module's context managers -- like usingwith
statements -- won't solve this either because they only care about what main program "owns" the thread.Furthermore, threads only get cleaned up or deleted when the main python program exits (to the best of the interpreter's ability -- see the paragraph about "dummy/alien threads" just above this link). This is why we need to better manage the threads that are created.
Enter the multiprocessing module... More on this when I get my hands dirty. As it is the drivetrain is stable, so I'll be starting a new branch to do some preliminary tests.
The text was updated successfully, but these errors were encountered: