...

/

Asynchronous to Synchronous Problem

Asynchronous to Synchronous Problem

A real-life interview question asking to convert asynchronous execution to synchronous execution.

We'll cover the following...

Asynchronous to Synchronous Problem

This is an actual interview question asked at Netflix.

Imagine we have an AsyncExecutor class that performs some useful task asynchronously via the method execute(). In addition, the method accepts a function object that acts as a callback and gets invoked after the asynchronous execution is done. The definition for the involved classes is below. The asynchronous work is simulated using sleep. A passed-in call is invoked to let the invoker take any desired action after the asynchronous processing is complete.

Executor Class

class AsyncExecutor:

    def work(self, callback):
        # simulate asynchronous work
        time.sleep(5)
        # let the invoker take action in a callback
        callback()

    def execute_async(self, callback):
        Thread(target=self.work,
...