(序言:我使用python-telegram-bot
运行一个Telegram bot,它在Google表单中注册用户的消息。这些都与这个问题无关,但可以提供一些背景来理解问题的根源。问题是googlesheetsapi不允许过于频繁地访问googlesheets,因此如果许多用户试图在那里编写,我需要延迟处理他们的请求)。
我知道使用threading
模块处理任务并避免被GIL锁定被认为是一种非常糟糕的做法。但是根据我的任务的性质,我接收到来自用户的请求流,我希望处理这些请求时有一定的延迟(比如比实际接收晚1到10秒)。(现在我用celery+redis来处理延迟的任务,但是对于延迟执行这样的小事来说,这看起来是一种过度的杀伤力,但是我可能错了)。你知道吗
因此,我想知道我是否可以使用concurrent.futures.ProcessPoolExecutor
(这里举例说明:https://idolstarastronomer.com/two-futures.html),或者它将导致大多数警告不要在Python中使用threading
的人所承诺的某种灾难?你知道吗
下面是纯粹假设的代码,它使用ProcessPoolExecutor
延迟运行某些东西。在某些情况下(比如太多的延迟请求),它会以灾难告终吗?你知道吗
import concurrent.futures
import time
import random
def register_with_delay():
time.sleep(random.randint(0, 10))
print('Im in the delayed registration')
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
futures = [executor.submit(register_with_delay) for _ in range(10)]
for i in range(10):
print('Im in the main loop')
time.sleep(random.randint(0, 1))
if __name__ == '__main__':
main()
目前没有回答
相关问题 更多 >
编程相关推荐