title: Extracting relationship status from Facebook in Python running on Google App Engine
body: <p>I'm working on a Facebook application. It's written in Python and running on Google App Engine (GAE).</p>
  
  <p>I know how to get the relationship status of the users network, but the way I do it is running way to slow. There is a time limit on GAE in order to reduce CPU usage which is about 60 seconds. Using the following code I can get the relationship status of about 200 persons in that time frame, before getting the "DeadlineExceededError". Of course that is not good enough, since many/most people have a higher amount of friends on Facebook.</p>
  
  <p>What I do now, is to get the friend list of the user, and then making an API call for each one asking for the name and relationship status.</p>
  
  <p><strong>The code I use:</strong></p>
  
  <pre><code>for f in friends:
      friends_info[f] = facebook.api('/'+f, {u'fields': 'name,relationship_status', 
      u'access_token': facebook.access_token})
  </code></pre>
  
  <p>And then I get the data with:</p>
  
  <pre><code>rel_status = friends_info[f][u'relationship_status']
  </code></pre>
  
  <p><strong>What I need:</strong></p>
  
  <p>Suggestions for optimization or other ways to extract the relationship status of an entire network.</p>
  
  <p>Thanks in advance :-)</p>
  
  <p><strong>EDIT :</strong></p>
  
  <p>The answers seems to be all good ways to solve my problem. Though, I couldn't find any good tutorials for batch request in Python, so I ended up using a FQL call. By using the following it takes 1-2 seconds to get the data:</p>
  
  <pre><code>Q = 'select uid, sex, relationship_status from user where uid in (select target_id from connection where source_id = me())'
  </code></pre>
  
  <p>And the actual data is retrieved by:</p>
  
  <pre><code>data = fql.query(Q,facebook.access_token)
  </code></pre>
  
  <p>which call this function:</p>
  
  <pre><code>def query(self,Q,access_token):
      ENDPOINT = 'https://api.facebook.com/method/'
      params = dict(query=Q, access_token=access_token, format='json')
      url = ENDPOINT + 'fql.query'
      return self._fetch(url, params=params)
  
  def _fetch(self, url, params=None):
      conn = urllib2.urlopen(url, data=urlencode(params))
      try:
          return json.loads(conn.read())
      finally:
          conn.close()  
  </code></pre>
  
  <p>Hope someone can make use of it as well!</p>