0

I'd like to use method assesion.run(), which expects pointers to async functions. Number of functions differs, so I tried to make a list of functions and spread it in .run() arguments section. But when appending them to list, they immediately invoke. How to generate variable number of functions please?

from requests_html import AsyncHTMLSession
asession = AsyncHTMLSession()

urls = [
    'https://python.org/',
    'https://reddit.com/',
    'https://google.com/'
    ]

async def get_html(url):
    r = await asession.get(url)
    return r

functionList = []

for url in urls:
    functionList.append(get_html(url))

# results = asession.run(fn1, fn2, fn3, fn4, ...)
results = asession.run(*functionList)

for result in results:
    print(result.html.url)
4
  • Use either threading module or concurrent.futures model to build out a que. Commented Oct 27, 2021 at 21:11
  • 1
    sounds kind of like functionList.append(lambda url=url: get_html(url)) Commented Oct 27, 2021 at 22:27
  • yes. I prefer partial but is the same. Commented Oct 28, 2021 at 2:46
  • @Ry- wow, I was thinking about lambda but never thought about such application of it. Thanks! Commented Oct 28, 2021 at 6:51

1 Answer 1

1

You have to pass the async functions not the coruoutines. The run function calls the arguments to create the coruoutines and then create the tasks.

So when you pass the coroutines run invoke them instead of creating tasks.

The functions are called without argument, so you should use functools.partial to put in the argument:

from functools import partial


...
for url in urls:
    functionList.append(partial(get_html, url))
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.