import concurrent.futures import os import subprocess import tempfile base = '/www/backup/txiaw-mysql-fix-20260831' root = tempfile.mkdtemp(prefix='cache-concurrency-', dir=base) php = '/www/server/php/56/bin/php' source = r''' require $argv[1]; $directory = $argv[2]; $loader = function () use ($directory) { file_put_contents($directory . '/loader-calls', "called\n", FILE_APPEND | LOCK_EX); usleep(100000); return array(array('news_id' => 42, 'news_name' => 'Concurrency fixture')); }; $result = txiaw_home_news_cached($directory, $loader); if ($result[0]['news_id'] !== 42) { exit(2); } echo "ok\n"; ''' def worker(_): return subprocess.run([php, '-n', '-r', source, base + '/stage/HomeNewsCache.php', root], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, timeout=5) with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool: results = list(pool.map(worker, range(8))) assert all(p.returncode == 0 and p.stdout.strip() == 'ok' for p in results), [(p.returncode, p.stdout, p.stderr) for p in results] calls = open(root + '/loader-calls').read().splitlines() assert len(calls) == 1, 'Concurrent cold requests executed %s loader calls' % len(calls) print('PASS: 8 simultaneous cold requests returned the same data with exactly 1 loader call.')