From: The Danny Bos on
Heya,

I'm running a py script that simply grabs an image, creates a
thumbnail and uploads it to s3. I'm simply logging into ssh and
running the script through Terminal. It works fine, but gives me an
IOError every now and then.

I was wondering if I can catch this error and just get the script to
start again?
I mean, in Terminal it dies anyway, so I have to start it again by
hand, which is a pain as it dies so sporadically. Can I automate this
error, catch it and just get it to restart the loop?

Thanks for your time and energy,

Danny
From: Chris Rebert on
On Sun, Jul 11, 2010 at 7:15 PM, The Danny Bos <dannybos(a)gmail.com> wrote:
> Heya,
>
> I'm running a py script that simply grabs an image, creates a
> thumbnail and uploads it to s3. I'm simply logging into ssh and
> running the script through Terminal. It works fine, but gives me an
> IOError every now and then.
>
> I was wondering if I can catch this error and just get the script to
> start again?
> I mean, in Terminal it dies anyway, so I have to start it again by
> hand, which is a pain as it dies so sporadically. Can I automate this
> error, catch it and just get it to restart the loop?

Of course. Use try-except;
http://docs.python.org/tutorial/errors.html#handling-exceptions
Here's one way to do it:

for image in images:#or whatever the actual loop is
# whatever
while True:
try:
part_that_may_raise_IOError()
except IOError:
print("IOError; Retrying...")
else:
break
# whatever other stuff

Cheers,
Chris
--
http://blog.rebertia.com
From: MRAB on
The Danny Bos wrote:
> Heya,
>
> I'm running a py script that simply grabs an image, creates a
> thumbnail and uploads it to s3. I'm simply logging into ssh and
> running the script through Terminal. It works fine, but gives me an
> IOError every now and then.
>
> I was wondering if I can catch this error and just get the script to
> start again?
> I mean, in Terminal it dies anyway, so I have to start it again by
> hand, which is a pain as it dies so sporadically. Can I automate this
> error, catch it and just get it to restart the loop?
>
> Thanks for your time and energy,
>
Exceptions can be caught. You could do something like this:

while True:
try:
do_something()
break
except IOError:
pass

From: Alex Hall on
It seems like seeing the code, or at least the bit in question, would
be easier, but what about:

for img in range(0, len(imagesToUpload:))
try:
#do the thumbnail / upload thing on images[i]
exceptIOError:
i=0

This will duplicate a lot of the images already processed, but you
said you are just restarting the script anyway. If you are in a while
loop, just move the processing to the for loop and use the while to
add all images to be processed to a list.

On 7/11/10, The Danny Bos <dannybos(a)gmail.com> wrote:
> Heya,
>
> I'm running a py script that simply grabs an image, creates a
> thumbnail and uploads it to s3. I'm simply logging into ssh and
> running the script through Terminal. It works fine, but gives me an
> IOError every now and then.
>
> I was wondering if I can catch this error and just get the script to
> start again?
> I mean, in Terminal it dies anyway, so I have to start it again by
> hand, which is a pain as it dies so sporadically. Can I automate this
> error, catch it and just get it to restart the loop?
>
> Thanks for your time and energy,
>
> Danny
> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
Have a great day,
Alex (msg sent from GMail website)
mehgcap(a)gmail.com; http://www.facebook.com/mehgcap
From: The Danny Bos on
Thanks gang,
I'm gonna paste what I've put together, doesn't seem right. Am I way
off?

Here's my code.
- It goes through a table Item
- Matches that Item ID to an API call
- Grabs the data, saves it and creates the thumbnail
- It dies due to Timeouts and Other baloney, all silly, nothing code
based.

items = Item.objects.all().filter(cover='').order_by('-reference_id')
for item in items:
url = "http://someaddress.org/books/?issue=%s" % item.reference_id

url_array = []
url_open = urllib.urlopen(url)
url_read = url_open.read().decode('utf-8')

try:
url_data = simplejson.loads(url_read)
url_array.append(url_data)

for detail in url_array:
if detail['artworkUrl']:
cover_url = detail['artworkUrl'].replace(' ','%20')
cover_open = urllib.urlretrieve(cover_url)
cover_name = os.path.split(cover_url)[1]

item.cover.save(cover_name, File(open(cover_open[0])),
save=True) ## Create and save Thumbnail

print "Cover - %s: %s" % (item.number, url)
else:
print "Missing - %s: %s" % (item.number, url)

except ValueError:
print "Error Processing record: %s: %s" % (item.reference_id, url)
pass
except IOError:
print "IOError; Retrying..."
pass

print "Done"



On Jul 12, 12:33 pm, MRAB <pyt...(a)mrabarnett.plus.com> wrote:
> The Danny Bos wrote:
> > Heya,
>
> > I'm running a py script that simply grabs an image, creates a
> > thumbnail and uploads it to s3. I'm simply logging into ssh and
> > running the script through Terminal. It works fine, but gives me an
> > IOError every now and then.
>
> > I was wondering if I can catch this error and just get the script to
> > start again?
> > I mean, in Terminal it dies anyway, so I have to start it again by
> > hand, which is a pain as it dies so sporadically. Can I automate this
> > error, catch it and just get it to restart the loop?
>
> > Thanks for your time and energy,
>
> Exceptions can be caught. You could do something like this:
>
>      while True:
>          try:
>              do_something()
>              break
>          except IOError:
>              pass