0

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

4
  • 1
    Check this thread out: stackoverflow.com/questions/11632034/… A better solution would be to use django-celery which helps queuing tasks taking long time. Commented May 17, 2019 at 7:44
  • 1
    you can convert it into a API view and use ajax to load the values later. A better way would be to save the converted values in separate columns in db itself so that you don't need to calculate them again and again. Commented May 17, 2019 at 7:51
  • I could make 1 request , then calculate actual rate and then construct dict barely by multiplying prices in list to rate, but i just interested in making it assync. Commented May 17, 2019 at 7:53
  • 1
    I would say, you can maintain a model which store the conversion rates mapped against currecies. then you can use celery to update that table couple of times(periodic task). Then you can use the model's conversion rate to convert value instead of making it async Commented May 17, 2019 at 8:25

1 Answer 1

0

finaly i decided calculate exchange rate 1 time and then use it to convert rest of the prices. Still quite slow though.

    def get_context_data(self, **kwargs):
        context = DetailView.get_context_data(self, **kwargs)
        context["blocket"], context['pricelist'] = (spider(self.kwargs.get("name")))
        rate = currency_converter(1000)
        context["pricelist_euro"] = []
        for price in context['pricelist']:
                try:
                    context["pricelist_euro"].append(int(price/rate))
                except TypeError:
                    context["pricelist_euro"].append(None)
        return context
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.