Aysnc Python dragging balls compared to Node.js

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Aysnc Python dragging balls compared to Node.js



Intro



I've been trying to reimplement as script I wrote in node.js into python. For some reason the python script is insanely slow compared to the node one (about 1/40th on my network). I'm new to python and especially asynchronous python. I don't think my python script is running asynchronously but I can't find any examples online of people trying to achieve similar functionality.



Scripts Function:



Contraints:



MusicBrainz api limits to 1 request/sec (why I use rate limiters in the scripts).



Node.js Implementation (Fast AF)


const config =
musicbrainz:
base_url: 'https://musicbrainz.org/ws/2/release',
num_pages: 1000,
releases_per_page: 100
,
coverartarchive:
base_url: 'https://coverartarchive.org/release',

;

const request = require('request');
const RateLimiter = require('limiter');

const removeTokens = (limiter,tokens) => new Promise(resolve => limiter.removeTokens(tokens,resolve));

const limiter = new RateLimiter(
1,
'second'
);

function getCAAurl(mbid)
return new Promise((resolve,reject)=>
request(

url: `$config.coverartarchive.base_url/$mbid`,
json: true
,
(err,res,body) =>
if(err) reject(err);
// No cover art available
if(res.statusCode === 404) return;
console.log(body.images[0].image)

)

);


function getMBIDS(pageIndex)
return new Promise((resolve,reject)=>
request(

url: config.musicbrainz.base_url,
json: true,
headers:
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
,
qs:
query: '*',
type: 'album',
format: 'Vinyl',
limit: config.musicbrainz.releases_per_page,
offset: pageIndex*config.musicbrainz.releases_per_page,
fmt: 'json'

,
(err,res,body) =>
if(err) reject(err);
resolve(body.releases.map(release=>release.id))

)
);


function start()
for (let pageIndex = 0; pageIndex < config.musicbrainz.num_pages;pageIndex++)
removeTokens(limiter,1)
.then(()=>getMBIDS(pageIndex))
.then(mbids=>
mbids.forEach(mbid=>
getCAAurl(mbid);
);
).catch(err=>
console.log(err);
process.exit(1);
);



start();



Python Implementation (Slow AF)


config =
"musicbrainz":
"base_url": "https://musicbrainz.org/ws/2/release",
"num_pages": 1000,
"releases_per_page": 100
,
"coverartarchive":
"base_url": "https://coverartarchive.org/release"



import aiohttp
import asyncio
from ratelimit import limits,sleep_and_retry

async def getCAAurl(mbid,session):
while True:
try:
async with session.get("%s/%s" % (config["coverartarchive"]["base_url"],mbid)) as resp:
resp_json = await resp.json();
print(resp_json['images'][0]['image'])
except (aiohttp.client_exceptions.ContentTypeError):
# No Cover Art Available
if(resp.status == 404):
break
except (aiohttp.ServerDisconnectedError):
continue
break

@sleep_and_retry
@limits(calls=1, period=1)
async def getMBIDs(page_index,session):
async with session.get(
"%s?query=*&type=album&format=Vinyl&limit=%s&offset=%s&fmt=json" %
(config["musicbrainz"]["base_url"],config["musicbrainz"]["releases_per_page"],page_index*config["musicbrainz"]["releases_per_page"])
) as resp:

mbids = map(lambda x: x['id'], (await resp.json())['releases']);

async with aiohttp.ClientSession() as caa_session:
caa_mbid_pages =
for mbid in mbids:
caa_mbid_pages.append(await getCAAurl(mbid,caa_session))

await asyncio.wait(caa_mbid_pages)

async def start():
async with aiohttp.ClientSession() as session:
mb_pages =
for page_index in range(config["musicbrainz"]["num_pages"]):
mb_pages.append(await getMBIDs(page_index,session))

await asyncio.wait(mb_pages)

loop = asyncio.get_event_loop()
loop.run_until_complete(start())









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS