Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Await resolvers in parallel #11

Merged
merged 2 commits into from
Sep 22, 2018
Merged

Await resolvers in parallel #11

merged 2 commits into from
Sep 22, 2018

Conversation

mawa
Copy link
Contributor

@mawa mawa commented Sep 21, 2018

This change allows for more parallelism when executing a query. Instead of sequentially awaiting all the affected resolvers of an object, kick off all the co-routines and then wait for them to finish using asyncio.gather(). Effectively, this means you will just wait for the slowest resolver instead of the accumulated time required for all resolvers.

As an illustration, consider the following code which runs twice as fast after this patch is applied:

def async expensive_rpc(*args):
  await asyncio.sleep(1.0)
  return 'hi!'

schema = GraphQLSchema(
  query=GraphQLObjectType(
    name='Root',
    fields={
      'foo': GraphQLField(
        GraphQLString,
        resolve=expensive_rpc,
       ),
       'bar': GraphQLField(
        GraphQLString,
        resolve=expensive_rpc,
       ),
    }
  )
)

await graphql(schema, '{foo, bar'}')

@mawa
Copy link
Contributor Author

mawa commented Sep 21, 2018

Oops. Broke the tests, I'll investigate. Please excuse the noise.

@Cito
Copy link
Member

Cito commented Sep 21, 2018

Thanks, @mawa. You're right, currently the fields are not resolved in parallel, and using gather() would solve that. I think you need to pass return_exceptions=False, since we want errors to pop up. And maybe you need to fix the order of errors in some tests (which is irrelevant) or fix the test to not check the order (maybe sorting both result and expected). Also, I would just return dict(await gather(...)) instead of using a dict comprehension.

@coveralls
Copy link

coveralls commented Sep 21, 2018

Coverage Status

Coverage increased (+0.1%) to 96.644% when pulling e3c32a9 on mawa:gather into 3de6454 on graphql-python:master.

@mawa
Copy link
Contributor Author

mawa commented Sep 21, 2018

Thanks for your feedback @Cito! I went with your suggestion and sorted the exceptions in the test suite before comparing them. This seems to work okay but the implementation is not perfect (had to convert the relevant properties of GraphQLError to a dict).

@Cito Cito merged commit efa9e82 into graphql-python:master Sep 22, 2018
@Cito
Copy link
Member

Cito commented Sep 22, 2018

Thanks for the contribution. I'll see if I can simplify the tests a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants