Poor interaction with threads (python 2.7)
还没有人认领这个 Issue。
评估
调研方向
从附带的 query.txt 和示例中显示的 GraphQLView.get_context 入口点开始,然后跨并发请求跟踪 dummy_middleware、嵌套 resolver、promise.async_instance 和 Flask g。使用提供的 Python 2.7 示例重现故障,并确定执行在哪个位置切换了线程。完成的标准是理解这种交互,并就保留请求本地上下文的修正方向达成一致。
由索引模型根据 Issue 内容生成。
描述
the interaction between this library, graphql-core, and promise appears to allow execution to hop threads especially (only?) when using a middleware and nested resolvers. this is inconsistent with flasks threading model and the ability to access the request/g thread locals
with the attached (very contrived) example, when submitting the query with concurrent requests the requests frequently fail because the key created in get_context doesn't exist on the threadlocal flask.g object in the resolvers. This happens when a thread accesses the promise.async_instance which isn't thread local, and resolves a promise that was created on a different thread.
query.txt
from flask import Flask
from flask_graphql import GraphQLView
app = Flask(__name__)
import graphene
import threading
import time
from flask import g
def get_user(info):
return g.get(info.context['key'])
class User(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
friend = graphene.Field(lambda: User)
age = graphene.Int()
apple = graphene.String()
@classmethod
def resolve_friend(cls, root, info):
time.sleep(.1)
x = get_user(info)
return User(id=id(root), name=':'.join([x.name, threading.current_thread().name]))
@classmethod
def resolve_age(cls, root, info):
time.sleep(.1)
return 5
@classmethod
def resolve_apple(cls, root, info):
time.sleep(.1)
return "Apple"
class Query(graphene.ObjectType):
me = graphene.Field(User)
def resolve_me(self, info):
time.sleep(.1)
return get_user(info)
schema = graphene.Schema(query=Query)
ahh = {}
def dummy_middleware(next, root, info, **args):
return_value = next(root, info, **args)
return return_value
import random
random.seed()
class TestQLView(GraphQLView):
def get_context(self, request):
# set a random key in g to be used by resolvers
key = str(random.randint(0,50))
name = threading.current_thread().name
user = User(id=key, name=name)
setattr(g, key, user)
return {
'key': key
}
app.add_url_rule('/graphql', view_func=TestQLView.as_view('graphql', schema=schema, graphiql=True, middleware=[dummy_middleware]))
- 主要语言
- Python
- 星标
- 1.3k
- 派生
- 139
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
graphql-python/flask-graphql 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 25/100
graphql-python/flask-graphql#92 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 25/100
-
难度 2/5 1-3 小时 新手友好度 35/100
graphql-python/flask-graphql#85 · 2 条评论 · 1 个 reaction ·
-
CSRF Exemption? 未关闭
难度 3/5 1-2 天 新手友好度 25/100
graphql-python/flask-graphql#84 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
graphql-python/flask-graphql#80 · 9 条评论 · 2 个 reaction ·
查看 graphql-python/flask-graphql 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
anthropics/skills#1811 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
speaches-ai/speaches#678 ·
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
datalayer/mcp-compose#42 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
conda-forge/spacy-feedstock#177 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2523 ·