Last active
January 18, 2021 10:18
Star
You must be signed in to star a gist
Сравниваем стратегии паджинации в Битрикс24
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from asyncio import gather | |
from contextlib import contextmanager | |
from time import monotonic | |
from fast_bitrix24 import Bitrix | |
from fast_bitrix24.utils import http_build_query | |
from more_itertools import chunked | |
bitrix = Bitrix('ХХХХХХХХХХХХ', | |
verbose=False) | |
@contextmanager | |
def timer(label): | |
t1 = monotonic() | |
yield | |
t2 = monotonic() | |
print(label, round(t2 - t1, 2), 'sec.') | |
async def batch_start_increment(bitrix: Bitrix, method, pages): | |
# подготавливаем список команд | |
cmds = { | |
i: method + '.list?' + http_build_query( | |
{'order': {'ID': 'ASC'}, | |
'select': ['*', 'UF_*'], | |
'start': i * 50}) | |
for i in range(pages) | |
} | |
# нарезаем список команд на батчи | |
batches_list = [] | |
for cmd_ID_batch in chunked(cmds, 50): | |
batch = {'halt': 0, | |
'cmd': {cmd_ID: cmds[cmd_ID] for cmd_ID in cmd_ID_batch}} | |
batches_list.append(batch) | |
return await gather( | |
*{bitrix.srh.single_request('batch', batch) for batch in batches_list}) | |
async def ID_filter(bitrix: Bitrix, method, pages): | |
result = [] | |
max_ID = 0 | |
for _ in range(pages): | |
page = await bitrix.srh.single_request( | |
method + '.list', | |
params={ | |
'select': ['*', 'UF_*'], | |
'start': -1, | |
'order': {'ID': 'ASC'}, | |
'filter': {'>ID': max_ID} | |
}) | |
max_ID = max(page.result, key=lambda x: x['ID']) | |
result.extend(page.result) | |
return result | |
def list_and_get(bitrix: Bitrix, method, pages): | |
return bitrix.get_by_ID(method + '.get', | |
get_IDs_for_method(method)[:pages * 50]) | |
IDs = {} | |
def get_IDs_for_method(method): | |
if method not in IDs: | |
with timer("Getting ID list for the 'list+get' strategy, " | |
f"method {method}:"): | |
IDs[method] = [ | |
item['ID'] for item in | |
bitrix.get_all(method + '.list', params={'select': ['ID']})] | |
print('') | |
return IDs[method] | |
def pages_test(method, pages): | |
print(f'Getting {pages} pages:') | |
with timer('ID filter:'): | |
bitrix.srh.run(ID_filter(bitrix, method, pages)) | |
with timer('Start increment:'): | |
bitrix.srh.run(batch_start_increment(bitrix, method, pages)) | |
with timer('List + get:'): | |
list_and_get(bitrix, method, pages) | |
print('') | |
for pages in [1, 50, 100, 200]: | |
pages_test('crm.lead', pages) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment