如何保持一个Python TCP监听应用程序运行?
How to keep a Python TCP listener application running?
我正在尝试制作一个 Python 程序,它将 运行 在 AWS(通过 Elastic Beanstalk 部署)上侦听 STOMP queue (a simple text based protocol over TCP). I'm using the stomp.py library, docs here.
这是程序,我运行使用 PyCharm 'Run application' 功能对其进行了调整:
# 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 秒,然后它停止 运行ning 并输出:
Process finished with exit code 0
如果注释掉 sleep
函数,它 运行 是主要函数,则立即完成该过程。
但是如果你在 Python 解释器中写出它,它会继续无限期地打印出队列消息。
如何让程序从 PyCharm 'Run application' 选项中无限期保留 运行ning?或者当我弄清楚如何部署程序时,AWS Elastic Beanstalk 会处理这个问题吗?
我对 Python 很生疏,对部署实际的 Python 应用程序还很陌生,如果这很明显,我深表歉意 question/answer。
您可以使用无限循环
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')
while True:
time.sleep(30)
while 循环将 运行,您的程序将休眠 30 秒,while 循环将再次 运行 然后程序将再次休眠 30 秒。由于没有条件终止循环,此过程将无限继续
我正在尝试制作一个 Python 程序,它将 运行 在 AWS(通过 Elastic Beanstalk 部署)上侦听 STOMP queue (a simple text based protocol over TCP). I'm using the stomp.py library, docs here.
这是程序,我运行使用 PyCharm 'Run application' 功能对其进行了调整:
# 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 秒,然后它停止 运行ning 并输出:
Process finished with exit code 0
如果注释掉 sleep
函数,它 运行 是主要函数,则立即完成该过程。
但是如果你在 Python 解释器中写出它,它会继续无限期地打印出队列消息。
如何让程序从 PyCharm 'Run application' 选项中无限期保留 运行ning?或者当我弄清楚如何部署程序时,AWS Elastic Beanstalk 会处理这个问题吗?
我对 Python 很生疏,对部署实际的 Python 应用程序还很陌生,如果这很明显,我深表歉意 question/answer。
您可以使用无限循环
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')
while True:
time.sleep(30)
while 循环将 运行,您的程序将休眠 30 秒,while 循环将再次 运行 然后程序将再次休眠 30 秒。由于没有条件终止循环,此过程将无限继续