def main(toprocess, subscription, refresh): sub = "projects/{0}/subscriptions/{1}".format(PROJECT_ID, subscription) r = Recurror(refresh - 10, postpone_ack) # pull() blocks until a message is received while True: try: #[START sub_pull] # Pull the data when available. resp = pubsub_client.projects().subscriptions().pull( subscription=sub, body={ "maxMessages": toprocess }).execute() #[END sub_pull] if resp: # Get the amount of media that one instance can processed # For this demo, we keep it to one per instance. m = resp.get('receivedMessages')[0] if m: message = m.get('message') ack_id = m.get('ackId') msg_string = base64.b64decode(message.get('data')) msg_data = json.loads(msg_string) bucket = msg_data["bucket"] filename = msg_data["name"] filetype = msg_data["type"] fn = filename.split('.')[0] Logger.log_writer("Started with {0}".format( str(msg_data["name"]))) print "Started:" + str(msg_data["name"]) # Start refreshing the acknowledge deadline. r.start(ack_ids=[ack_id], refresh=refresh, sub=sub) Logger.log_writer("{0} process starts".format(filename)) start_process = datetime.datetime.now() # <Your custom process> m = Process(bucket, filename, filetype, PROJECT_ID) filetype = str(filetype) if filetype.startswith('image') == True: Logger.log_writer("IMAGE FILE FOUND") print "IMAGE FILE FOUND" content = m.img_to_text() if len(content[filename]) == 0: print 'Food Name not found' Logger.log_writer('Food Name not found') ingredients = [] ingredients.append("Sorry ! We couldn't help.") print m.upload_local_image( "/tmp/food/noresult.jpg") else: searchTerm = content[filename][0].encode('utf-8') ingredients = crawl.find(searchTerm) print ingredients if len(ingredients) == 0: print 'Food Details not found' Logger.log_writer('Food Details not found') ingredients.append("Sorry ! We couldn't help.") print m.upload_local_image( "/tmp/food/noresult.jpg") else: m.getFirstImage(searchTerm) writeResponse = m.upload_object(ingredients) print "WRITE Response:" print(json.dumps(writeResponse, indent=2)) elif filetype.startswith('text') or filetype.startswith( 'application/octet-stream'): Logger.log_writer("TEXT FILE FOUND") print "TEXT FILE FOUND" coordinates = m.get_object() print coordinates m.find_suggestions(coordinates[0], coordinates[1]) else: Logger.log_writer( "{0} of {1} filetype not supported".format( str(filename), filetype)) print("{0} of {1} filetype not supported".format( str(filename), filetype)) # <End of your custom process> end_process = datetime.datetime.now() Logger.log_writer("{0} process stops".format(filename)) #[START ack_msg] # Delete the message in the queue by acknowledging it. pubsub_client.projects().subscriptions().acknowledge( subscription=sub, body={ 'ackIds': [ack_id] }).execute() #[END ack_msg] # Logs to see what's going on. Logger.log_writer( "{media_url} processed by instance {instance_hostname} in {amount_time}" .format(media_url=msg_string, instance_hostname=INSTANCE_NAME, amount_time=str(end_process - start_process))) # Stop the ackDeadLine refresh until next message. r.stop() except Exception, e: print("Exception found: {0}".format(str(e)))