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

Add clarification around concurrency and parallelization #314

Closed
jnak opened this issue Oct 4, 2016 · 7 comments
Closed

Add clarification around concurrency and parallelization #314

jnak opened this issue Oct 4, 2016 · 7 comments

Comments

@jnak
Copy link

jnak commented Oct 4, 2016

Hey guys,

I've been wondering what is the concurrency behavior / story of graphene but I don't see anything mentioned in the code base.

A few questions on my mind:

  • Has it been a design constrain yet at all? Was the API / architecture designed with this potential concurrency down the road?
  • Can any schema be resolved in parallel in a thread pool? like most sql drivers do
  • Can we also use gevent or twisted for I/O calls that support it? like making network calls
  • Can we mix and match the 2 approaches?

Since Python does not really shine out-of-the-box when it comes to parallelization, I think having clarity would help contributors to build performant integrations with different frameworks. Also it would help users to decide to use Graphene or not.

In my case, I could potentially use the base NodeJS implementation to call over RPCs into my python services, but we are mostly a python shop and I don't want to introduce yet another language (esp Node :p ) unless I really need to.

If concurrency hasn't been given yet much thought but you think it should, especially in the v1 context, I would be very happy to brainstorm here with the broader community :)

Cheers,
J

@morgante
Copy link

morgante commented Oct 4, 2016

I'd love to know more about this story as well. We're working on moving some large projects to Graphene, but need to have good answers before doing so.

@syrusakbary
Copy link
Member

Hi @jnak @morgante,

Graphene is completely ready to handle concurrency/parallelization. Under the hood is using graphql-core which few months ago was improved to be capable to use Promise abstraction in Python.

Here's the related PR: graphql-python/graphql-core#59

Thanks to using Promises we would be able to abstract the way functions are executed and finished independently of the execution engine/event loop.

The most important thing is the application logic and Graphene types and resolvers should remain the same independently of the executor used.

Available executors

By default, graphql will use a SyncExecutor. However, there are more executors available:

  • graphql.execution.executors.asyncio.AsyncioExecutor: This executor executes the resolvers in the Python asyncio event loop.
  • graphql.execution.executors.gevent.GeventExecutor: This executor executes the resolvers in the Gevent event loop.
  • graphql.execution.executors.process.ProcessExecutor: This executor executes each resolver as a process.
  • graphql.execution.executors.thread.ThreadExecutor: This executor executes each resolver in a Thread.
  • graphql.execution.executors.sync.SyncExecutor: This executor executes each resolver synchronusly (default).

How you can use it with Graphene?

from graphql.execution.executors.thread import ThreadExecutor

schema = graphene.Schema(query=...)
schema.execute(query, executor=ThreadExecutor())

Python 3.5

If you are using Python 3.5 Graphene should be able to resolve your async/await functions in the asyncio event loop too with no logic changes in your app ;)
Here are some tests demonstrating this in graphql-core.

PS: Sorry this is not documented yet in the website.

@jnak
Copy link
Author

jnak commented Oct 6, 2016

Wow! That's much more that I expected!

That's really awesome. Thanks for implementing this and sharing it here. I'm going to be toying with the Gevent executer next week and we'll report back. If everything works fine, I'll definitely try to send a PR to put this in the doc. Hopefully, in the meantime, people interested in this topic will find this answer.

On a related note, since Gevent uses an event loop, it is probably feasible to implement something similar to Dataloader in the JS world. This is especially useful for the 1+N^2 fan out problem. Have you heard of anyone interested in doing this as well?

@DeoLeung
Copy link

DeoLeung commented Oct 9, 2017

Hi,
I'm trying to get into the GraphQL world.

I want to use Graphene as a standalone server, do I still need to scale up like normal REST backend by deploying multiple instances?

I'm using tornado at the moment, it looks like if I use the AsyncioExecutor, it'll run on only one eventloop, so I still need multiple tornado instances?

@jkimbo
Copy link
Member

jkimbo commented Feb 18, 2018

Hi @jnak . We're currently going through old issues that appear to have gone stale (ie. not updated in about the last 6 months) to try and clean up the issue tracker. If this is still important to you please comment and we'll re-open this.

Thanks!

@fhennig
Copy link

fhennig commented Jan 8, 2020

Hi @jkimbo , this issue is still important to me and I think it is important to add this to the official documentation. When googling "graphene parallelization" this is the first and pretty much only thing that comes up.

Furthermore I'd like to know how to make my code thread safe when using the ThreadExecutor (I'm assuming the context object needs to be thread safe?)

@rdpravin1895
Copy link

Hi @syrusakbary, the link for tests does not exist anymore. Do you have an updated link, or is this info about concurrency available in the docs? I don't find it in the docs though

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

No branches or pull requests

7 participants