1

What is the best way to make an asynchronous call appear synchronous? Eg, something like this, but how do I coordinate the calling thread and the async reply thread? In java I might use a CountdownLatch() with a timeout, but I can't find a definite solution for Python

def getDataFromAsyncSource():
    asyncService.subscribe(callback=functionToCallbackTo)
    # wait for data
    return dataReturned

def functionToCallbackTo(data):
    dataReturned = data
2
  • What library are you using for the async calls?
    – schlamar
    Commented May 25, 2012 at 13:43
  • It's a proprietary library which interfaces to Bloomberg. It's standard pub/sub.
    – ashbyp
    Commented May 26, 2012 at 19:10

2 Answers 2

5

There is a module you can use

import concurrent.futures

Check this post for sample code and module download link: Concurrent Tasks Execution in Python

You can put executor results in future, then get them, here is the sample code from http://pypi.python.org:

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
    'http://www.cnn.com/',
    'http://europe.wsj.com/',
    'http://www.bbc.co.uk/',
    'http://some-made-up-domain.com/']

def load_url(url, timeout):
    return urllib.request.urlopen(url, timeout=timeout).read()

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    future_to_url = dict((executor.submit(load_url, url, 60), url)
                     for url in URLS)

    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        if future.exception() is not None:
            print('%r generated an exception: %s' % (url,future.exception()))
        else:
            print('%r page is %d bytes' % (url, len(future.result())))
2
  • 1
    More generally, it depends on the functionality of the asynchronous execution technology OP is using.
    – Marcin
    Commented May 25, 2012 at 7:47
  • Link to docs: docs.python.org/dev/library/…
    – Michal
    Commented Nov 3, 2013 at 8:46
4

A common solution would be the usage of a synchronized Queue and passing it to the callback function. See http://docs.python.org/library/queue.html.

So for your example this could look like (I'm just guessing the API to pass additional arguments to the callback function):

from Queue import Queue

def async_call():
    q = Queue()
    asyncService.subscribe(callback=callback, args=(q,))
    data = q.get()
    return data

def callback(data, q):
    q.put(data)

This is a solution using the threading module internally so it might not work depending on your async library.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.