title: GCP PubSub: Synchronous Pull Subscriber in Python?
body: <p>All,</p>
  
  <p>I'm trying to learn how to use GCP PubSub, and I'm able to test it out via the CLI commands (create topics, subscriptions, publish to topic, pull from subscription, etc.), however when I jump over to python (v 2.7, current company standard) I am struggling with pulling the messages in a synchronous fashion.</p>
  
  <p>I've reviewed this url, which tells you to do a sleep and While True, but I can't imagine anyone is doing that in the real world, right?
  <a href="https://cloud.google.com/pubsub/docs/quickstart-client-libraries#pubsub-subscribe-python" rel="nofollow noreferrer">https://cloud.google.com/pubsub/docs/quickstart-client-libraries#pubsub-subscribe-python</a></p>
  
  <p>This url tells you you can use future.result(), which I tried but it doesn't block the session/thread like you'd think:
  <a href="http://google-cloud-python.readthedocs.io/en/latest/pubsub/subscriber/index.html" rel="nofollow noreferrer">http://google-cloud-python.readthedocs.io/en/latest/pubsub/subscriber/index.html</a></p>
  
  <p>anyone have any other ideas?  Here is my function, which is straight out of one of the examples pretty much:</p>
  
  <pre><code>def sample_receive_messages(subscription_name="my-sub", project=None):
  """Receives messages from a pull subscription."""
  if not project:
      credentials, project = google.auth.default()  
  
  subscriber = psub.SubscriberClient()
  subscription_path = subscriber.subscription_path(
      project, subscription_name)
  
  def callback(message):
      # print('Received message: {}'.format(message))
      message.ack()
      print('&lt;message&gt;' + str(message.data) + '&lt;/message&gt;')
  
  subscription = subscriber.subscribe(subscription_path)
  
  future = subscription.open(callback)
  
  myResult = future.result()
  
  subscription.close()
  
  print("done")
  </code></pre>
  
  <p>My goal at the end of this is to have a process that wakes up every so often, grabs messages and ACKs them, writes the messages out to a file, then ends.</p>
  
  <p>As of now, the process reads the messages and prints them out (great), but it then sits and sits and sits and finally errors out with some gibberish:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "pubSubTools.py", line 50, in sample_receive_messages
      myResult = future.result()
    File "/usr/local/lib/python2.7/site-packages/google/cloud/pubsub_v1/futures.py", line 98, in result
      raise err
  grpc._channel._Rendezvous: &lt;_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, The service was unable to fulfill your request. Please try again. [code=8a75])&gt;
  </code></pre>