我使用libtorrent(python)下载许多种子(大约1500-从100mb到几个gb)。 我尝试了其他设置,但在24小时内无法下载4-5以上的文件。
这些任务可能有特殊设置吗?
这是代码的最新版本:
设置会话:
async def lt_session(app):
ses = app['lt_session'] = lt.session({
'active_downloads': 50,
})
ses.listen_on(6881, 6881)
ses.add_extension('ut_metadata')
ses.add_extension('smart_ban')
ses.add_extension('ut_pex')
ses.add_extension('metadata_transfer')
ses.start_dht()
ses.start_lsd()
app['torrents'] = {}
开始下载:
async def ensure_downloads_loop(app):
while True:
await asyncio.sleep(300)
try:
await ensure_downloads(app)
except:
pass
async def ensure_downloads(app):
pool = app['db_pool'][xxx]
sql = "SELECT post_id FROM xxx WHERE status IN ('new', 'downloading') order by id ASC;"
async with pool.acquire() as conn:
async with conn.cursor(DictCursor) as cur:
await cur.execute(sql)
items = await cur.fetchall()
result = {
'downloading': 0,
'downloaded': 0,
}
for item in items:
id = item['post_id']
handle = app['torrents'].get(id)
if handle is None:
handle = await run_download(app, id)
done = handle.status().state == lt.torrent_status.seeding
if not done:
result['downloading'] += 1
continue
pool = app['db_pool'][xxx]
sql = '''
UPDATE xxx
SET status = 'downloaded'
WHERE post_id = {id};
'''.format(id=id)
async with pool.acquire() as conn:
async with conn.cursor(DictCursor) as cur:
await cur.execute(sql)
await conn.commit()
if handle:
app['lt_session'].remove_torrent(handle)
del app['torrents'][id]
await check_post_is_done(app, id)
result['downloaded'] += 1
return result
开始下载torrent:
async def run_download(app, id):
id = int(id)
pool = app['db_pool'][xxx]
sql = 'SELECT * FROM xxx WHERE id = %d;' % id
async with pool.acquire() as conn:
async with conn.cursor(DictCursor) as cur:
await cur.execute(sql)
item = await cur.fetchone()
ses = app['lt_session']
path = os.path.join(DOWNLOADS, str(id))
if not os.path.exists(path):
os.makedirs(path)
params = {'save_path': path}
handle = lt.add_magnet_uri(ses, item['magnet'], params)
handle.set_download_limit(-1)
handle.set_upload_limit(10000)
app['torrents'][id] = handle
return handle