本文整理汇总了Python中promise.dataloader.DataLoader方法的典型用法代码示例。如果您正苦于以下问题:Python dataloader.DataLoader方法的具体用法?Python dataloader.DataLoader怎么用?Python dataloader.DataLoader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类promise.dataloader
的用法示例。
在下文中一共展示了dataloader.DataLoader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_supports_loading_multiple_keys_in_one_call
# 需要导入模块: from promise import dataloader [as 别名]
# 或者: from promise.dataloader import DataLoader [as 别名]
def test_supports_loading_multiple_keys_in_one_call():
def call_fn(keys):
return Promise.resolve(keys)
identity_loader = DataLoader(call_fn)
promise_all = identity_loader.load_many([1, 2])
assert isinstance(promise_all, Promise)
values = promise_all.get()
assert values == [1, 2]
promise_all = identity_loader.load_many([])
assert isinstance(promise_all, Promise)
values = promise_all.get()
assert values == []
示例2: id_loader
# 需要导入模块: from promise import dataloader [as 别名]
# 或者: from promise.dataloader import DataLoader [as 别名]
def id_loader(**options):
load_calls = []
resolve = options.pop("resolve", Promise.resolve)
def fn(keys):
load_calls.append(keys)
return resolve(keys)
identity_loader = DataLoader(fn, **options)
return identity_loader, load_calls
示例3: test_build_a_simple_data_loader
# 需要导入模块: from promise import dataloader [as 别名]
# 或者: from promise.dataloader import DataLoader [as 别名]
def test_build_a_simple_data_loader():
def call_fn(keys):
return Promise.resolve(keys)
identity_loader = DataLoader(call_fn)
promise1 = identity_loader.load(1)
assert isinstance(promise1, Promise)
value1 = promise1.get()
assert value1 == 1
示例4: test_dataloader_thread_safety
# 需要导入模块: from promise import dataloader [as 别名]
# 或者: from promise.dataloader import DataLoader [as 别名]
def test_dataloader_thread_safety():
"""
Dataloader should only batch `load` calls that happened on the same thread.
Here we assert that `load` calls on thread 2 are not batched on thread 1 as
thread 1 batches its own `load` calls.
"""
def load_many(keys):
thead_name = threading.current_thread().getName()
return Promise.resolve([thead_name for key in keys])
thread_name_loader = DataLoader(load_many)
event_1 = threading.Event()
event_2 = threading.Event()
event_3 = threading.Event()
assert_object = {
'is_same_thread_1': True,
'is_same_thread_2': True,
}
def task_1():
@Promise.safe
def do():
promise = thread_name_loader.load(1)
event_1.set()
event_2.wait() # Wait for thread 2 to call `load`
assert_object['is_same_thread_1'] = (
promise.get() == threading.current_thread().getName()
)
event_3.set() # Unblock thread 2
do().get()
def task_2():
@Promise.safe
def do():
promise = thread_name_loader.load(2)
event_2.set()
event_3.wait() # Wait for thread 1 to run `dispatch_queue_batch`
assert_object['is_same_thread_2'] = (
promise.get() == threading.current_thread().getName()
)
do().get()
thread_1 = threading.Thread(target=task_1)
thread_1.start()
event_1.wait() # Wait for thread 1 to call `load`
thread_2 = threading.Thread(target=task_2)
thread_2.start()
for thread in (thread_1, thread_2):
thread.join()
assert assert_object['is_same_thread_1']
assert assert_object['is_same_thread_2']
示例5: test_batches_correctly
# 需要导入模块: from promise import dataloader [as 别名]
# 或者: from promise.dataloader import DataLoader [as 别名]
def test_batches_correctly(executor):
# type: (SyncExecutor) -> None
Business = GraphQLObjectType(
"Business",
lambda: {
"id": GraphQLField(GraphQLID, resolver=lambda root, info, **args: root)
},
)
Query = GraphQLObjectType(
"Query",
lambda: {
"getBusiness": GraphQLField(
Business,
args={"id": GraphQLArgument(GraphQLNonNull(GraphQLID))},
resolver=lambda root, info, **args: info.context.business_data_loader.load(
args.get("id")
),
)
},
)
schema = GraphQLSchema(query=Query)
doc = """
{
business1: getBusiness(id: "1") {
id
}
business2: getBusiness(id: "2") {
id
}
}
"""
doc_ast = parse(doc)
load_calls = []
class BusinessDataLoader(DataLoader):
def batch_load_fn(self, keys):
# type: (List[str]) -> Promise
load_calls.append(keys)
return Promise.resolve(keys)
class Context(object):
business_data_loader = BusinessDataLoader()
result = execute(schema, doc_ast, None, context_value=Context(), executor=executor)
assert not result.errors
assert result.data == {"business1": {"id": "1"}, "business2": {"id": "2"}}
assert load_calls == [["1", "2"]]