【发布时间】:2014-03-20 02:24:22
【问题描述】:
在这段代码中,我只是从队列中拉出并在无限循环中处理消息。 因此,我通过 API 弹出基于 REST 的队列,解析消息,然后通过 Twilio 发送一条短信,并一次又一次地无限次地执行此操作。 我正在考虑规模,所以我想最大限度地利用时间并让这个循环在每个脚本中同时运行几次。
如何将此代码转换为同时运行 4 次,而不必执行文件四次。
.....
var = 1
while var == 1 : # This constructs an infinite loop
try:
queue = CHECKQ.queue("name_of_queue")
msgs = queue.get(max=1, timeout=None)
url = msgs['messages'][0]['body']
msgId = msgs['messages'][0]['id']
delMsg = queue.delete(msgId)
number = "1"+url
message = client.messages.create(to=number, from_="+15555555555",
body="Here's the link to install the controller -
http://someapp.com")
except (IndexError):
sleep(1)
【问题讨论】:
-
你看过 Python 的 multiprocessing 模块吗?
-
谢谢,会申请的。可能自己回答这个。
-
顿悟...并发循环
标签: python multithreading parallel-processing python-multithreading