gpt4 book ai didi

python - google app engine——异步代码块? Python

转载 作者:行者123 更新时间:2023-11-28 18:33:28 25 4
gpt4 key购买 nike

所以我有一些代码的功能类似于这个通用代码:

for blah in blahs:
blah.take_forever()

for blah2 in blah2s:
vars = blah2.take_forever()
for var in vars:
var.also_take_forever()

我想要一些功能类似于异步的东西,例如

async_start_blah2_loop_then_do_someting_else()

do_the_first_blah_loop()

gather_results_and_send_them_out()

但是,我没有为此使用数据存储或 urlfetch,那么还有哪些其他选项可以加快此过程?

map_async(callback, pass_batch_into_callback=None, merge_future=None, **q_options)
Asynchronously map a callback function or tasklet over the query results. This is the asynchronous version of map().

似乎只适用于数据存储查询。

建议?

最佳答案

你可以使用 Python 的原生 threading App Engine 中的模块异步执行函数(参见 threading.Thread )。这也适用于自动缩放实例,因为它使用“绿色”线程而不是操作系统线程。

def blahFunc():
for blah in blahs:
blah.take_forever()

def blah2Func():
for blah2 in blah2s:
vars = blah2.take_forever()
for var in vars:
var.also_take_forever()

# Execute both loops 'at the same time'
background = threading.Thread(target=blah2Func)
background.start()
blah1Func()

请注意,您不一定会因此获得任何速度(为了简单起见,我假设您的示例是人为设计的示例),因为请求仍在同一个“真实”操作系统线程中执行,但如果您需要避免阻塞,它很有用长期运作。

如果您有真正长时间运行的任务,可能需要运行比请求更长的时间,更好的解决方案是使用 Task Queues .

关于python - google app engine——异步代码块? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34733200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com