Page MenuHomePhabricator

Make bot operate concurrently
Closed, ResolvedPublic

Description

Both of T420294 and T415882 will make editing slower if completed. Concurrency needs to be introduced to counteract that effect.
See https://wikitech.wikimedia.org/wiki/Robot_policy and https://www.mediawiki.org/wiki/Wikimedia_APIs/Rate_limits for rate limits.
Docs regarding authenticating to the REST API: https://www.wikidata.org/wiki/Wikidata:REST_API/Authentication

Details

Related Changes in GitLab:
TitleReferenceAuthorSource BranchDest Branch
Make bot operate concurrently and restructure delinter codetoolforge-repos/delintbot!25r4356thconcurrentmain
Customize query in GitLab

Event Timeline

Redmin triaged this task as Medium priority.Mar 27 2026, 11:13 AM
Redmin moved this task from Triage to Future Work on the Tool-delintbot board.
Redmin claimed this task.

Done along with these other notable changes:

  • Got rid of 'mode' argument
  • Removed the 'cat' global variable
  • Introduced a new custom lint category '0' which tells the bot/delinter to fix errors across all lint categories
  • Div-span-flip fixes no longer change attributes (perhaps the most significant gain of the delinter restructuring effort)
  • Made Playwright share the same browser instance
  • Add link to my fork of mw-api-client in the USER-AGENT

The reason why I ended up going with ThreadPoolExecutor (even though I had written the code using ProcessPoolExecutor at first) is that this little benchmark:

import time, asyncio
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from multiprocessing import get_context
from delinter import delint
from query import querydb
from mw_api_client import Wiki
from config import USERAGENT
from os import environ
from utils import infer_wiki_url

DBNAME = 'wikidatawiki'
LINT   = '22' 
NSID   = '4'
LIMIT  = 200

# 1. Get titles from the linter DB
titles = querydb(
    DBNAME,
    f"SELECT DISTINCT page_title FROM linter "
    f"JOIN page ON page.page_id = linter.linter_page "
    f"WHERE linter_cat = {LINT} AND linter_namespace = {NSID} "
    f"AND linter_template = '' "
    f"ORDER BY page_title DESC LIMIT {LIMIT};"
)
titles.pop(0)
# 2. Fetch the actual wikitext for each
wiki = Wiki(infer_wiki_url(DBNAME) + "api.php", USERAGENT)
SAMPLES = [str(wiki.page(('Wikidata:' + t).strip()).read()) for t in titles]
N = len(SAMPLES)
print(f"# {N} real pages, total {sum(map(len, SAMPLES))/1024:.0f} KiB, "
      f"median {sorted(map(len, SAMPLES))[N//2]} bytes\n")

async def run_all(executor, label):
    loop = asyncio.get_running_loop()
    t0 = time.perf_counter()
    await asyncio.gather(*[
        loop.run_in_executor(executor, delint, s, LINT) for s in SAMPLES
    ])
    dt = time.perf_counter() - t0
    print(f"{label:<22} {dt:7.3f}s   {N/dt:8.1f} pages/s")
    return dt

async def main():
    for s in SAMPLES[:5]:
        delint(s, LINT)

    with ProcessPoolExecutor(max_workers=3, mp_context=get_context('fork')) as >
        pp.submit(delint, SAMPLES[0], LINT).result()  # start workers
        await run_all(pp, "ProcessPool x3")

    with ThreadPoolExecutor(max_workers=3) as tp:
        await run_all(tp, "ThreadPool  x3")

    await run_all(None, "Inline (no pool)")

if __name__ == '__main__':
    asyncio.run(main())

produced this result:

# 199 real pages, total 1569 KiB, median 4026 bytes

ProcessPool x3           0.298s      668.7 pages/s
ThreadPool  x3           0.198s     1005.9 pages/s
Inline (no pool)         0.299s      665.3 pages/s

Looks like going with multiple processes would have added too much overhead.