0

Im trying to run this function every 2seconds forever :

import requests
from bs4 import BeautifulSoup
import asyncio

async def scrape():
    test = []
    r = requests.get(coin_desk)
    soup = BeautifulSoup(r.text, features='xml') 
    title = soup.find_all('title')[2]
    await asyncio.sleep(2)
    for x in title:
        test.append(x)
        print(test)

if name == 'main':

try:
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(scrape())
except (KeyboardInterrupt, SystemExit):
    pass

If i use

run_forever()

instead of

run_until_complete(scrape())

nothing gets printed out, it just runs forever and skips the function it seems.

3
  • 1
    Do you get any error messages with an exception / stack trace? Have you tried adding other prints to see which lines you actually reach before it exits? Commented Nov 18, 2022 at 22:03
  • No error messages, when i try to use run_forever() nothing gets printed out. It just runs forever and skips the function completly. Commented Nov 18, 2022 at 22:27
  • the async routine (scrape) was never placed into the runtime/evloop. run_until_complete includes the parameter to queue a routine for you Commented Nov 18, 2022 at 22:48

1 Answer 1

0

The async routine (scrape) was never placed into the event loop.

You can add it with

loop.create_task(routine)

See: What does asyncio.create_task() do?

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.