Hi everyone,
Is it possible to launch a python command without waiting for a return ? and therefore start the next command without waiting the previous one to finish ?
I searched and asked also on
StackOverflow but so far I didn't found what I was looking for...
something that look promising might be the
concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
as seen here ->
https://stackoverflow.com/a/60832288/11943028
But this example is focus on results that I don't need. any ideas ?
Thank you.
SpongeBob Wrote:but so far I didn't found what I was looking for...
It is quite unclear what you are looking for. If you want to start function
spam()
and do something else while spam() is running, you can simply start a thread that runs spam(). If you want to use a ThreadPoolExecutor to start the thread, why not? Why wouldn't this suit your needs? Have you tried anything? Post your code!
Merci @
Gribouillis ;)
Quote: If you want to start function spam() and do something else while spam() is running,
Yes it's what I would like to achieve.
But it not need to be constrain for functions only.
and a solution where the return of that function / code is not mandatory.
Because in all the example that I could find I see always liked to a return.
I was thinking to use ThreadPoolExecutor or ProcessPoolExecutor (
depending if it's CPU or IO usage)
but I don't find a good document/tutorial where I could use those.
If you have any example that will be appreciate.
Thank for your help.
SpongeBob Wrote:If you have any example that will be appreciate.
You could perhaps check the Pymotw3 about
concurrent.futures
The easy way would be to run it in a thread:
import threading
def do_something_and_move_on():
pass
worker = threading.Thread(target=do_something_and_move_on)
worker.start()
# now do other things while it runs...
# then join the worker, to make sure it's done before the script ends
worker.join()
The more complicated answer would be to write your code using async/await, and let the asyncio library handle it for you. But that's harder to do if you already have code that you don't want to mess with.
Thank you very much @nilamo !
I like simple code to demonstrate how it work.
After the worker.join() Can I retrieve a return value from the worker ?
@all
What is the main difference between those two modules ?
threading
concurrent
(Jun-15-2020, 10:40 PM)nilamo Wrote: [ -> ]import threading
....
Nilamo, by any chance did you have a simple example like this one but with
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
?
Because it really easy to switch thread/Process execution with this module. Thanks.
There are 9 simple examples like this one on the pymotw3 page I linked above...
I tried this
import pyttsx3
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
def talk(txt):
engine = pyttsx3.init()
engine.say(txt)
engine.runAndWait()
engine.stop()
return 'done'
executor = ThreadPoolExecutor(max_workers=20)
future = executor.submit(talk('This will be, a long text for testing purpose.'))
print('This text should appear before or during the TTS')
print(future.result())
At the moment 'This text should appear before or during the TTS' only appear once the tts engine is finished :/
and I have a bunch of error on the last line (print(future.result()))
Thanks.
I think it should be
future = executor.submit(talk, 'This will be, a long text for testing purpose.')
You don't call the function. Just pass the function and its arguments to
submit
.