I'm trying to make a Python program that will run on AWS (deployed via Elastic Beanstalk) that listens to a STOMP queue (a simple text based protocol over TCP). I'm using the stomp.py library, docs here.
Here's the program, I'm running it with the PyCharm 'Run application' feature:
# 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()
It works nicely, printing out the queue messages, but only for 30 seconds, then it stops running and outputs:
Process finished with exit code 0
If you comment out the sleep
function it runs the main function, then immediately finishes the process.
But if you write it out in the Python interpreter it continues to print out the queue messages indefinitely.
How do I get the program to keep running indefinitely from the PyCharm 'Run application' option? Or will AWS Elastic Beanstalk take care of that when I figure out how to deploy the program?
I'm quite rusty with Python, and new to deploying actual Python apps, so apologies if this an obvious question/answer.
You can use an infinite while loop
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)
The while loop will run, your program will sleep for 30 seconds, the while loop will run again and then the program will sleep again for 30 seconds. This process will continue infinitely as there is no condition to terminate the loop