0

I am writing a program to capture motion, take a picture, sleep, blink etc. Here is the code so far without the functions because those aren't the problem.

try:
   starting()
   while duration < 3:
          previous_state = current_state
          current_state = GPIO.input(sensor)
          if current_state != previous_state:
                  new_state = "High" if current_state else "low"
                  if current_state:
                           blink_led(5)
                           pic = '/home/pi/pic%s.jpg' % num
                           num += 1
                           camera.capture(pic)
                           data = open(pic, 'rb')
                           s3.Bucket('bucket').put_object(Key=pic, Body=data)
                           sleep(5)
                           duration += 1

The problem here is that I want it to sleep for 5 seconds but if the upload takes a while then it is basically sleeping for more than 5 seconds. Depending on my internet connection. I have tried

while s3.Bucket('bucket').put_object(Key=pic, Body=data):
     sleep(5)
     duration += 1

but then it gets stuck in an infinite loop and if I do an if clause at the end like:

if duration == 3:
     s3.Bucket('bucket').put_object(Key=pic, Body=data)

it will only upload the last photo. I have also tried doing at the end:

if duration == 3:
      s3.Bucket('bucket').put_object(Key='/home/pi/pic1.jpg', Body=data)
      s3.Bucket('bucket').put_object(Key='/home/pi/pic2.jpg', Body=data)
      s3.Bucket('bucket').put_object(Key='/home/pi/pic3.jpg', Body=data)

It can only upload the first then it gets hung up. How should I write the uploading as a background process so it doesn't sleep longer than 5 seconds? I feel like I have tried everything other than asynchronous which I think is specific to the raspberry pi

John Raesly
  • 308
  • 4
  • 11
  • use an asynchronous interface to upload or move blocking IO operations into a background thread, to avoid freezing your main loop. – jfs May 21 '16 at 03:22
  • I thought about an asynchronous interface to upload so I would call that method within so I would still get my 3 photos? I understand above but any tutorials for this part: move blocking IO operations into a background thread, to avoid freezing your main loop. That sort of sounds like the opposite of above – John Raesly May 21 '16 at 04:22
  • The latter is how you could implement the former e.g., you could use a thread pool, to upload files concurrently: `async_result = pool.apply_async(upload_file, (pic,))` where `pool` is created once in the beginning: `pool = multiprocessing.dummy.Pool(10)`. – jfs May 21 '16 at 07:27
  • If you could provide a complete example as an answer I'll update and mark it but I don't understand where you got 10 vs 5 – John Raesly May 21 '16 at 11:34
  • Look up the docs for `multiprocessing` stdlib module. If it is still unclear what the code in my comment does; ask a specific question. – jfs May 21 '16 at 12:25
  • It takes 1 photo then it uploads then it sleeps for 5 seconds and does it 3 times then quits but again it can get hung up on uploading the photo. That is what I would like be a background process. – John Raesly May 21 '16 at 13:35
  • Before it sleeps I want it to upload to my s3 – John Raesly May 21 '16 at 14:07

0 Answers0