3

I'm creating the asynchronous function call in my application to process the task(function).

I have tried with threadpoolexecutor as shown below for asynchronous call but it is not working as expected, kindly let me know What I have done wrongly?

class MainTest:

    def __init__(self): pass


    def show_msg(self):
        print('inside show msg function..!')
        time.sleep(3)

    def executor_call(self):
        executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
        executor.submit(obj.show_msg())
        executor.shutdown(wait=False)
        print('Hi')

obj = MainTest()

obj.executor_call()

I am expecting above code output like

Hi

inside show msg function..!

but I am getting

inside show msg function..!

Hi
zwer
  • 24,943
  • 3
  • 48
  • 66
Ramesh
  • 51
  • 1
  • 8
  • What you get is the intended behavior, why would you expect the opposite? – Thierry Lathuille Dec 25 '18 at 08:36
  • I would like run some set of code parallely(seperate thread) from main thread.So when I submit the executor.submit it will continue to next line with out waiting the result of the executor. – Ramesh Dec 25 '18 at 08:59

1 Answers1

2

You're calling your function immediately instead of executing it through the ThreadPoolExecutor. This line:

executor.submit(obj.show_msg())

Is functionally equivalent to:

result = obj.show_msg()
executor.submit(result)

What ThreadPoolExecutor.submit() expects from you is to pass it a callable which it will execute in a separate thread, so change the call to:

executor.submit(obj.show_msg)

That being said, even when you fix it, you still won't be getting your expected behavior unless you move your time.sleep(3) before printing of your message in the show_msg() function.

Also, keep in mind that using threads does not give you parallel execution, it gives you concurrent execution for all things happening within the Python realm (system calls like I/O can happen in parallel) due to the dreaded GIL. You'll need to employ multiprocessing if you want a proper parallel execution.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • Thanks for the reply zwer, but I have seen from below link https://stackoverflow.com/questions/51502812/why-doesnt-concurrent-futures-threadpoolexecutor-submit-return-immediately , we can able to get the scenario which I am looking using threadpoolexecutor . – Ramesh Dec 25 '18 at 09:19
  • @Ramesh - You can get to the scenario you're looking for with the above fixes, but it will not be running in parallel, it will run concurrently (that's why the whole package is called `concurrent`). Check [this question](https://stackoverflow.com/questions/1050222/what-is-the-difference-between-concurrency-and-parallelism) to understand the difference. – zwer Dec 25 '18 at 09:26
  • ok,Thank you , I should not put there as parallely sorry, I am expecting Asynchronous execution of the function.but after making the code change as you said result = obj.show_msg() executor.submit(result),still I am getting the same behavior. – Ramesh Dec 25 '18 at 09:36
  • @Ramesh - Did you move `time.sleep(3)` before `print('...')` in the `show_msg()` function? If you don't do that chances are that the thread executing `show_msg()` will get to the `print('...')` faster than your main thread as it still has to finish setting up the `ThreadPoolExecutor`. – zwer Dec 25 '18 at 09:39
  • yes, I have moved the time.sleep before print statement.class MainTest: def __init__(self): pass def show_msg(self): time.sleep(3) print('inside show msg function..!') def executor_call(self): executor = concurrent.futures.ThreadPoolExecutor(max_workers=1) result = obj.show_msg() executor.submit(result) executor.shutdown(wait=False) print('Hi') obj = MainTest() obj.executor_call() – Ramesh Dec 25 '18 at 09:42
  • @Ramesh - Don't use the example with `result ...` - it's just there to demonstrate what's happening and why are you not getting your expected result. Just use `executor.submit(obj.show_msg)` instead of `executor.submit(obj.show_msg())` in your existing code. – zwer Dec 25 '18 at 09:51
  • Could you please look into this below issue which I am facing and let me know if possible.https://stackoverflow.com/q/54381767/4640211 – Ramesh Jan 28 '19 at 03:59