1
def A(event):
   B(event)
   return "something"

def B(event)
   return event

Let's say I have a function like the one above, how do make function A return its output and not wait for function B. The output of A does not depend on function B but function B depends on the input of function A.

1
  • I don't think python-asyncio is a valid tag here? since it's supposed to be used for questions related to the library asyncio, not necessarily asynchronous programming. Commented Aug 29, 2018 at 0:40

1 Answer 1

1

In your example most easy way to do it is to run B in a thread. For example:

import time
from concurrent.futures import ThreadPoolExecutor


executor = ThreadPoolExecutor(max_workers=1)


def A():
    executor.submit(B)
    print("Hi from A")

def B():
    time.sleep(1)
    print("Hi from B")


if __name__ == '__main__':
    A()

If you want to do it using asyncio, you should wrap A and B to be coroutines and use asyncio.Task. Note however that unless B is I/O related it wouldn't be possible to make it coroutine without using thread. Here's more detailed explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. Your solution worked for me. Very grateful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.