0

I know how to implement an async operation in Java like this

Instantiate a new thread -> thread do time-consuming stuff(disk IO or network activity) -> thread finishes its job and pass result to main thread

I'm trying to do the same thing in python but could not find a way... I have read about async and await, multiprocessing.Pool. Neither gives me an clear idea on how to achieve the same thing.

The nearest way I found is multiprocessing.Pool.apply_async(Callable,Iterable,Callback).But this thing cannot be instantiate in a class since it needs to be wrapped in if __name__=="__main__" and the __name__ is my class name.

Any elegant way to do async in Python? Thanks!

1 Answer 1

2

Thread Basics

Take a look at the threading module.

An example :

from threading import Thread

# instanciate a new thread
t = Thread(target=my_task)
# let it do the time-consuming stuff
t.start()
# do extra stuff while t is working
do_extra_stuff()
# wait for t to finish
t.join()

Getting Task Result

However, if you want to get a value from my_task, you'll need to either use global variable, mutable parameter or wrap your task into a class extanding Thread.

Here's an example with a wrapping class :

class MyThread(Thread):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.result = None

    def run(self):  # this is the Thread method you need to override
        # the time consuming stuff
        self.result = some_result

# same as before, but using the new class, so no need to specify target.
t = MyThread()
t.start() # will call t.run() for you. Never call t.run yourself (you'll lose every benefit from using Thread)
t.join() # wait for t to be finished before getting result
print(t.result)

Passing Arguments

To pass arguments to the task (including the callback), you can either put them as class fields and fill them in the construcor, or use the args kwarg from Thread's construcor, like this :

t = Thread(target=task_with_parametters, args=(arg1, arg2))

Thread will call task_with_parametters like this :

task_with_parametters(arg1, arg2)
Sign up to request clarification or add additional context in comments.

4 Comments

Can I implement a call back using using this method?
Edited with a new example, is this what you need ?
Thanks for helping! Partially yes, but the join still blocks the main thread, I need a call back mechanism which calls a function(this function is somehow defined when we construct the thread) when the thread finishes its job.
Edited with 2 possibilities on how to pass arguments (and therefore the callback) to the task.

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.