How to run Gunicorn concurrently with an infinite loop?

时间:2017-06-15 10:19:38

标签: python multithreading gunicorn bottle

I have a python script in which a loop needs to run forever. In the same script I run Gunicorn to run a bottle.py application. My problem is when I start the script from terminal console, either the Gunicorn web server runs or the infinite loop. Never both. If I make the script like this: (let's say version A)

Def run_forever()
    while true:
    '''check values of some variables and call other functions for actions'''

p_run_forever = threading.Thread(target=run_forever)
p_run_forever.start()
p_run_forever.daemon = True
run(host='0.0.0.0', port=80, server='gunicorn', workers=4)

The run_forever() runs only once and after Gunicorn starts , it never runs again. If I make the script like this: (let's say version B)

Def run_forever()
    while true:
    '''check values of some variables and call other functions for actions'''

run(host='0.0.0.0', port=80, server='gunicorn', workers=4)
p_run_forever = threading.Thread(target=run_forever)
p_run_forever.daemon = True # If this is false, the script gets locked and I have to close the terminal window
p_run_forever.start()

Gunicorn starts but the run_forever() never gets executed. I tried to put Gunicorn inside a thread but it complains that the signal can only run in the main thread. So what am I doing wrong? How can I make both Gunicorn and my infinite loop run simultaneously without blocking each other?

0 个答案:

没有答案