开发者

python facebook sdk call to facebook is slow when compared to command line curl

开发者 https://www.devze.com 2023-02-13 12:21 出处:网络
I am using facebook python Graph API. When i am calling put_object to write to news feed it i开发者_运维技巧s taking about 12-14 sec to complete the call. When i run from command line using curl with

I am using facebook python Graph API. When i am calling put_object to write to news feed it i开发者_运维技巧s taking about 12-14 sec to complete the call. When i run from command line using curl with same parameters i get the response back in 1.2 seconds.

I ran the profiler on the python code and from i see that it is spending 99.5% time in the socket.recv . I am not sure if it is the problem with facebook python sdk or something else.

I am on python 2.6. i see from facebook.py that it is using urllib.

    file = urllib.urlopen("https://graph.facebook.com/" + path + "?" +
                          urllib.urlencode(args), post_data)

Has someone experienced similar slow down ? Any suggestions will be highly appreciated.


Direct command-line CURL is bound to be faster than urllib or urllib2. If you want speed, you could replace the call using pycurl (which is also a C-extension) whereas urllib is a python module written on top of httplib.

What more you could do is, if you're flexible enough to use a Tornado server, use the async caller of Tornado which directly talks to sockets and is also asynchronous.

Also, if nothing out of these can be done, try replacing urllib with urllib2 and create a non blocking caller with callback returns. This is all that I've done to improve the native 3rd party wrappers of facebook/twitter/amazon etc.


Are you behind an http proxy server? Curl honors proxy server environment variables, while urllib doesn't do so by default, and also doesn't support calling an https url (such as https://graph.facebook.com) over a proxy server.

In any event I expect it's more likely a network issue than a Python vs C issue. Yes C is faster, but this isn't a CPU-bound task, and there's no way that you're burning 12-14 seconds inside the Python interpreter to make this call.

If curl is happy but urllib is not, perhaps trying pycurl will solve your problem. http://pycurl.sourceforge.net/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号