Threading
Note
See also concurrency for more information on multithreading.
You can use a ThreadPoolExecutor to speed up slow, I/O-heavy operations such as reading a block from disk.
import logging
from concurrent.futures import ThreadPoolExecutor
import pbk
logging.basicConfig(level=logging.INFO)
log = pbk.KernelLogViewer()
MAX_WORKERS = 1
READ_N_LAST_BLOCKS = 1000
def process_block(chainman: pbk.ChainstateManager, index: pbk.BlockTreeEntry):
block_data = chainman.blocks[index]
# implement block processing logic
# ...
print(f"Successfully processed block {index.height}")
chainman = pbk.load_chainman("/tmp/bitcoin/signet", pbk.ChainType.SIGNET)
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as pool:
for idx in chainman.get_active_chain().block_tree_entries[-READ_N_LAST_BLOCKS:]:
pool.submit(process_block, chainman, idx)