本文整理匯總了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"]]