markpipython
Posts: 2
Joined: Mon Dec 26, 2016 8:46 am

Pi Python script stops after 3-4 hours

Mon Dec 26, 2016 8:06 pm

Hi I'm new to Python and have made a simple script to record an integer value to a url (saving to Mysql database).
This script will run for 3 to 4 hours without a hitch, but then is stops responding!
Can anyone help, please. Thank you

Script
____________________________________________________________________________
#!/usr/bin/python

import requests
import time
import math

url = 'http://castle.remote-data.net/insert.php?one=89&two='

print 'Monitoring a variable every minute...'

while True:
result = 250 // Using value of 250 to test
print 'Variable: %i' % (result)

data = url+str(result)
r = requests.get(data, stream=True)
print (r.status_code) // We should get back '200'
time.sleep(60.0) // Wait one minute



Errors after it stops working
____________________________________________________________________________
Traceback (most recent call last):
File "atod.py", line 38, in <module>
r = requests.get(data, stream=True)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 60, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))

User avatar
paddyg
Posts: 2464
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi Python script stops after 3-4 hours

Tue Dec 27, 2016 12:45 am

you could put that bit inside a try, except block and check whether you've got a ConnectionError (or some other one I suppose). Maybe twiddle your fingers for a bit then try again.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2869
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: Pi Python script stops after 3-4 hours

Tue Dec 27, 2016 5:42 pm

paddyg wrote:you could put that bit inside a try, except block and check whether you've got a ConnectionError (or some other one I suppose). Maybe twiddle your fingers for a bit then try again.

Yep. I'd guess it's bombing out the first time it fails to connect to the url. Letting it fail gracefully with try: except: should work. :D
Alex Eames RasPi.TV, RasP.iO

markpipython
Posts: 2
Joined: Mon Dec 26, 2016 8:46 am

Re: Pi Python script stops after 3-4 hours

Thu Dec 29, 2016 9:48 pm

Thank you
I've used the 'try' and 'except' as suggested and been working fine now for a couple of days...

try:
r = requests.get(data, stream=True)
print ('Light: %i & Result: %i' % (result, r.status_code))
except ConnectionError as e:
print e
r = "No Response"

Return to “Python”