I would like to have your opinion on how to convert following function to asynchronous one.
def currency_converter(price, curr1="SEK", curr2="EUR"):
c = CurrencyConverter()
try:
return c.convert(price, curr1, curr2)
except ValueError or RateNotFoundError as err:
return str(err)
This function takes price, 2 currencies codes and convert price to the chosen currency. Problem is when you use this function in cycle, it takes a while to send and receive requests to / from web host for each iteration ( around 2-3 secs for 20 requests)
This function is used in the following VIEW in DJANGO:
class BlocketView(DetailView):
model = BoatModel
template_name = 'blocket.html'
def get_context_data(self, **kwargs):
context = DetailView.get_context_data(self, **kwargs)
context["blocket"], context['pricelist'] = (spider(self.kwargs.get("name")))
context["pricelist_euro"] = [currency_converter(price) for price in context['pricelist']]
return context
Here it gets prices from pricelist and makes new context["pricelist_euro"] list with the converted prices.
Also this function might be used as a template filter:
@register.filter
def currency_converter(price, curr1="SEK", curr2="EUR"):
c = CurrencyConverter()
try:
return c.convert(price, curr1, curr2)
except ValueError or RateNotFoundError as err:
return str(err)
Is it any chance to convert this function to asynchronous one somehow?
Thank you