1

Basically I'm using pyppeteer to connect to an existing browser connection which requires me to periodically time.sleep() the thread in order for the browser to behave normally (using asyncio.sleep() still causes dynamic HTML websites to behave funnily, I suspect it's to do with the underlying javascript detecting a puppeteer connection to the browser, something time.sleep() seems to block by (if I had to guess) temporarily pausing this connection)

What I need to be able to do is pause the part of the python telegram script thats connecting to the webpages similar to how time.sleep() does but without pausing all the other things the python telegram bot script is doing. I suspect I could do this by disconnecting from the browser connection and reconnecting but I suspect this would mess up the ordering of the current active pages (just from working with pyppeteer for a while it seems to be incapable of ordering webpages identically between browser connections, especially if the webpage titles are identical) and cause other errors when it comes to my code.

So to the actual question, can I pause parts of an asyncio event loop in a method which is functionally identical to a time.sleep() but isn't asyncio.sleep() as this doesn't seem to work, probably as it switches from doing the current task to maintaining the background threads which are dealing with the browser connection.

The reason python telegram bot is involved is that my code works by triggering the pyppeteer code from telegram using a command however whilst the thread is sleeping using time.sleep the bot is unable to respond to telegram commands due to the entire script being paused.

Jimmy J.K.
  • 25
  • 6
  • 2
    You could use multiple threads. Sleep in one thread won't block the other threads. – Barmar Jun 06 '22 at 20:35
  • Yeah that would probably work, but that does turn this into a game of making the code thread safe, which is an increase in work for something that I think can be answered through a clever utilization of pyppeteer underlying methods in some way. But yes for now I think I'll have to just multithread this, I'll leave the question up incase anyone comes up with an idea that doesn't involve multithreading. – Jimmy J.K. Jun 06 '22 at 20:44
  • 5
    There's no such thing as a free lunch. – Barmar Jun 06 '22 at 20:45

1 Answers1

0

can I pause parts of an asyncio event loop

If I understood your problem correctly, you should create multiple tasks and then apply asyncio.sleep where you want to pause that task, this will not affect other tasks. e.g.

async def task1():
    for i in range(5):
        print("task1: pausing task for 1 sec")
        await asyncio.sleep(1)
        print("task1: resumed again.")
        print("task1: doing some work ...")
        
async def task2():
    print("task2: working on ...")
    await asyncio.sleep(10)
    print("task2: work finished")

async def main():
    t1 = asyncio.create_task(task1())
    t2 = asyncio.create_task(task2())
    print("doing some other tasks in main coro")
    await asyncio.sleep(5)
    print("main coro tasks done. waiting for t1, t2 to finish")
    await asyncio.wait({t1,t2})
    
asyncio.run(main())

I could do this by disconnecting from the browser connection and reconnecting but I suspect this would mess up the ordering of the current active pages

If you think there would be no auto-reload on any page, then before disconnecting you can set some kind of attributes for you and when you connect back you can identify each page by its attr, like you can change the page titles, to use it later when get connected back.

Faizan AlHassan
  • 389
  • 4
  • 8