I'm currently using a daemon thread in Python to constantly detect parts of the screen in the background while other more important functions are running. testImToStr
is in the same file as the rest of the code I am using.
def testImToStr():
while True:
pospro.imagetoString();
doImageToString = threading.Thread(target=testImToStr, args=(), daemon=True)
doImageToString.start()
while True:
#other stuff i was too lazy to copy over
This version is working as it does both the image processing and the other stuff in the while loop. However, then the target thread is in another module:
doImageToString = threading.Thread(target=pospro.loopedImToStr, args=(), daemon=True)
doImageToString.start()
while True:
#other stuff i was too lazy to copy over
Other module:
def loopedImToStr():
while True:
imagetoString()
def imagetoString():
#stuff here
It only loops the target thread and does not run the while loop in the file that originally made the thread. How is it that both loops are run when the thread is in the same file as the loop but only the thread is run when it they are in different files?
I think all your problem makes the most common mistake - target
has to be function's name without ()
- so called callback
Thread(target=pospro.loopedImToStr, daemon=True)
and later Thread.start()
will use ()
to run it.
In your code you run testImToStr()
at once like
result = testImToStr()
doImageToString = threading.Thread(target=result, ...)
so testImToStr()
blocks all code and it can't run other loop.