Python Forum
Launch another python command without waiting for a return.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Launch another python command without waiting for a return.
#1
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.
[Image: NfRQr9R.jpg]
Reply
#2
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!
Reply
#3
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.
[Image: NfRQr9R.jpg]
Reply
#4
SpongeBob Wrote:If you have any example that will be appreciate.
You could perhaps check the Pymotw3 about concurrent.futures
Reply
#5
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.
Reply
#6
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
[Image: NfRQr9R.jpg]
Reply
#7
(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.
[Image: NfRQr9R.jpg]
Reply
#8
There are 9 simple examples like this one on the pymotw3 page I linked above...
Reply
#9
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.
[Image: NfRQr9R.jpg]
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Waiting for input from serial port, then move on KenHorse 3 1,002 Apr-17-2024, 07:21 AM
Last Post: DeaD_EyE
  pip stops waiting for python walker 6 1,047 Nov-28-2023, 06:55 PM
Last Post: walker
  Launch Python IDLE Shell from terminal Pavel_47 5 1,221 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Waiting for heavy functions question philipbergwerf 14 3,368 Apr-29-2022, 07:31 PM
Last Post: philipbergwerf
  How to create waiting process? samuelbachorik 4 1,971 Sep-02-2021, 05:41 PM
Last Post: bowlofred
  Launch Windows Application OEMS1 0 2,118 Mar-26-2021, 07:42 PM
Last Post: OEMS1
Photo Python won`t launch error newbieee 0 1,758 Feb-02-2021, 11:51 PM
Last Post: newbieee
  Waiting and listening test 2 2,136 Nov-13-2020, 04:43 PM
Last Post: michael1789
  waiting for barcode scanner output, while main program continues to run lightframe109 3 4,641 Sep-03-2020, 02:19 PM
Last Post: DeaD_EyE
  waiting to connect Skaperen 9 3,542 Aug-17-2020, 05:58 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020