Skip to content

Commit d2b5d1c

Browse files
committed
add parallel blocks downloading
1 parent 28fba44 commit d2b5d1c

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

mytoninstaller/settings.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import random
1010
import json
1111
import pkg_resources
12+
from concurrent.futures import ThreadPoolExecutor, as_completed
13+
1214

1315
from mypylib.mypylib import (
1416
add2systemd,
@@ -109,6 +111,12 @@ def is_testnet(local):
109111
return True
110112
return False
111113

114+
def download_blocks(local, bag: dict):
115+
local.add_log(f"Downloading blocks from {bag['from']} to {bag['to']}", "info")
116+
if not download_bag(local, bag['bag']):
117+
local.add_log("Error downloading archive bag", "error")
118+
return
119+
112120
def download_bag(local, bag_id: str):
113121
local_ts_url = f"http://127.0.0.1:{local.buffer.ton_storage.api_port}"
114122
downloads_path = '/tmp/ts-downloads/'
@@ -137,7 +145,7 @@ def update_init_block(local, seqno: int):
137145
local.add_log(f"Editing init block in {local.buffer.global_config_path}", "info")
138146
url = f'https://toncenter.com/api/v2/lookupBlock?workchain=-1&shard=-9223372036854775808&seqno={seqno}'
139147
if is_testnet(local):
140-
url.replace('toncenter.com', 'testnet.toncenter.com')
148+
url = url.replace('toncenter.com', 'testnet.toncenter.com')
141149
resp = requests.get(url).json()
142150
if not resp['ok']:
143151
local.add_log("Error getting init block from toncenter", "error")
@@ -186,11 +194,15 @@ def download_archive_from_ts(local):
186194
update_init_block(local, state_bag['at_block'])
187195

188196
local.add_log("Downloading archive blocks", "info")
189-
for bag in block_bags:
190-
local.add_log(f"Downloading blocks from {bag['from']} to {bag['to']}", "info")
191-
if not download_bag(local, bag['bag']):
192-
local.add_log("Error downloading archive bag", "error")
193-
return
197+
with ThreadPoolExecutor(max_workers=4) as executor:
198+
futures = [executor.submit(download_blocks, local, bag) for bag in block_bags]
199+
for future in as_completed(futures):
200+
try:
201+
future.result()
202+
except Exception as e:
203+
local.add_log(f"Error while downloading blocks: {e}", "error")
204+
return
205+
194206
local.add_log(f"Downloading blocks is completed", "info")
195207

196208
archive_dir = local.buffer.ton_db_dir + 'archive/'

0 commit comments

Comments
 (0)