我试图制作一个Python程序,该程序将运行在监听STOMP队列(基于TCP的简单文本协议)的AWS上(通过Elastic Beanstalk部署)。我正在使用stomp.py库,docs here。如何保持Python TCP侦听器应用程序运行?
这里的程序,我用PyCharm“运行应用程序的运行它的特点:
# PyPI imports
import stomp
# TODO: TESTING ONLY, REMOVE WHEN DONE!
import time
def main():
'''Starts the STOMP listener.'''
print('Running main function')
# See the following example for the template this file was based on:
# http://jasonrbriggs.github.io/stomp.py/quickstart.html
conn = stomp.Connection([('example.com', 61613)],
auto_decode=False)
conn.set_listener('print', stomp.PrintingListener())
conn.start()
conn.connect(username = 'username', passcode = 'password', wait=False)
conn.subscribe(destination='/destination',
id=1,
ack='auto')
time.sleep(30)
if __name__ == '__main__':
main()
它工作得很好,打印出队列中的消息,但只有30秒,然后停止运行,输出: Process finished with exit code 0
如果您注释掉sleep
函数,它将运行主函数,然后立即结束该过程。
但是,如果你在Python解释器中写出它,它将继续无限地输出队列消息。
如何让程序从PyCharm'Run application'选项无限期运行?或者,当我找出如何部署该程序时,AWS Elastic Beanstalk会如何处理?
我对Python很生疏,对于部署实际的Python应用程序很陌生,所以如果这是一个明显的问题/答案,请道歉。
有趣的技术,问题几个:(1)那个无限循环会不会消耗CPU资源吗? (2)这真的不像Python最佳实践方法,是吗? –
不,不会,因为你没有在循环体内做任何cpu密集工作......你只是暂停应用程序 – danidee
好吧,我想这是真的。像Flask和Django这样的框架如何保持其进程运行?如果他们甚至有长时间运行的过程,我想。 –