Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a python app repeatedly using Bluemix?

The Bluemix python build pack works just fine - very easy to cf push myapp --no-route. So far, so good.

A question, though:

I want to run it periodically on Bluemix as I would using cron on my local system.

Background

The app is not written as a long-running task. I just run it periodically to collect data from several websites that I have written for my clients and to email me results when appropriate.

When I run it on IBM Bluemix though, the Bluemix runtime currently thinks the app is failing when it exits and needs to be restarted immediately. This is not what I want, of course.

like image 368
iainH Avatar asked Jun 09 '26 03:06

iainH


1 Answers

There are a couple options:

1) If you want to do it completely in Python, you could try something like I did in this example. The basic structure is like this:

import schedule  
 import time 

 def job():  
 #put the task to execute here  

 def anotherJob():  
 #another task can be defined here  

 schedule.every(10).minutes.do(job)  
 schedule.every().day.at("10:30").do(anotherJob)  

while True:  
   schedule.run_pending()  
   time.sleep(1)  

2) Bluemix has changed and today a better approach would be to use OpenWhisk. It is IBM's version of "serverless" computing and it allows to schedule execution of tasks or do it event-driven. You could move your app into a Docker container and invoke it based on a schedule or driven by external events.

like image 134
data_henrik Avatar answered Jun 10 '26 17:06

data_henrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!