Skip to content

Instantly share code, notes, and snippets.

@leshchenko1979
Last active January 18, 2021 10:18
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save leshchenko1979/970ebad4006221633b7201bce2b1ee2c to your computer and use it in GitHub Desktop.
Сравниваем стратегии паджинации в Битрикс24
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