所以,我有一个小程序,看起来像这样:
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()
函数在另一个程序中导入,因此它与主程序不在同一个线程中。
我该怎么做?