cooperhewitt / label-book

Generates a markdown file for use with InDesign
1 stars 0 forks source link

Update to use ch.api.execute_request_multi #14

Open straup opened 9 years ago

straup commented 9 years ago

Both this:

https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L41-L46

And this:

https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L51-L57

Are candidates for parallel processing / HTTP requests.

That said this is currently blocked until the async branch of py-cooperhewitt-api is merged back in to master...

https://github.com/cooperhewitt/py-cooperhewitt-api/compare/async

straup commented 9 years ago

Okay, if you install cooperhewitt.api from the async branch you will be able to do the following:

import cooperhewitt.api.client

TOKEN = 'S33KR3T'
api = cooperhewitt.api.client.OAuth2(TOKEN)

reqs = (
    ('cooperhewitt.labs.whatWouldMicahSay', {}),
    ('cooperhewitt.objects.getRandom', {'has_image': 1}),
    ('cooperhewitt.labs.whatWouldMicahSay', {}),
    ('cooperhewitt.objects.getRandom', {'has_image': 1}),
    ('cooperhewitt.labs.whatWouldMicahSay', {}),
    ('cooperhewitt.objects.getRandom', {'has_image': 1}),
)

# rsp is a generator/iterator so it only yields one response
# at a time - there is no array slicing here...

rsp = api.execute_method_multi(reqs)

for r in rsp:
    print r

I need to have a bit more of a think about packaging before pushing this to master, specifically whether grequests should really be considered a core dependency or whether we move this in to a separate (sub-classed) library of its own.

In the meantime, if someone could tell me whether grequests both installs and runs on Heroku that would be helpful.

straup commented 9 years ago

Also this:

https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L41-L46

Could be updated as follows:

1) Call 'cooperhewitt.exhibitions.getInfo' first and note the object count, calculating the number of pages necessary to fetch all the objects

2) Call 'cooperhewitt.exhibitions.getObjects' using api.execute_method_multi and the pagination arguments calculated in (1)

straup commented 9 years ago

Okay, if you install py-cooperhewitt-api-multiclient you can do parallel API requests.

The interface is exactly the same but includes an additional execute_methods method that returns a generator of response hashes. See below for details.

Note that py-cooperhewitt-api-multiclient requires v0.4.4 of py-cooperhewitt-api which is available on GitHub but not pypi yet (cc @micahwalter).

115 ->python
Python 2.7.10 (v2.7.10:15c95b7d81dc, May 23 2015, 09:33:12) 
>>> import cooperhewitt.api.multiclient
>>> TOKEN = 'S33KRET'
>>> mc = cooperhewitt.api.multiclient.OAuth2(TOKEN)
>>> req = (('cooperhewitt.labs.whatWouldMicahSay', {}), ('cooperhewitt.labs.whatWouldMicahSay', {}))
>>> rsp = mc.execute_methods(req)
>>> for r in rsp:
...     print r
... 
{u'stat': u'ok', u'micah': {u'says': u'Getting into a little bit of trouble is important.'}}
{u'stat': u'ok', u'micah': {u'says': u'Nobody owns volunteers.'}}
... 
rsp = mc.execute_method(*req[1])
{u'stat': u'ok', u'micah': {u'says': u'That is such a bunch of charette.'}}
ghost commented 9 years ago

installed multiclient and trying to make multiple api calls but it seems to be running just as, if not more, slowly.

@straup could you have a look at this branch and let me know what you think?

https://github.com/cooperhewitt/label-book/blob/multiclient/label-book.py