如何制作异步延迟函数?

时间:2017-03-26 19:31:12

标签: python asynchronous

所以,我有一个小程序,看起来像这样:

global_cache = {}

def callback(tgi=""):
    if tgi is None:
        page_id = "0"
    else:
        page_id = tgi

    # something to fetch json data from an api to a "blob" object
    # then save the "blob" to the global cache
    blob = {'page_id': page_id, 'data': "some data"}
    global_cache['data'] = blob

    cache_cleaner('data')
    return blob


def cache_cleaner(string):
    time.sleep(60*60) # wait for one hour until it cleans the cache
    global_cache[string] = {}

嗯,正如您所看到的,callback()函数将不会返回blob数据,直到cache_cleaner()函数中的睡眠用完为止。但是我想要调用该函数并异步等待时间,因此callback()函数可以在cache_cleaner()的时间运行时返回数据。

此外,callback()函数在另一个程序中导入,因此它与主程序不在同一个线程中。

我该怎么做?

0 个答案:

没有答案