V
V
Vyacheslav Grachunov2021-07-21 02:25:23
Python
Vyacheslav Grachunov, 2021-07-21 02:25:23

How to solve such problem with pyppeteer, asyncio and pyrogram?

There is an event for the appearance of a new message, from which you need to call a screenshot on pyppeteer in a loop
Looks like this:

....
async def screenshot(stock, loop, date_msg):
    await launch(headless=True, defaultViewport=None, args=['--no-sandbox'])
    .....
....

@client.on_message()
def msg_handler(client, message):
    .......
   for row in result:
       loop = asyncio.get_event_loop()
       loop.run_until_complete(screenshot(stock.upper(), loop, date_msg))

    screens.append(InputMediaPhoto(os.path.join(BASEDIR, stock.upper()+".png")))


As such, I get the following error:
RuntimeError: There is no current event loop in thread 'Handler_0'


Because trying to run from a non-main thread

loop1 = asyncio.new_event_loop()
asyncio.set_event_loop(loop1)
loop = asyncio.get_event_loop()
loop.run_until_complete(screenshot(stock.upper(), loop, date_msg))

Here in this I get the following:
signal only works in main thread
This is already from pyppeteer
But if I turn off the signals:
await launch(headless=True, defaultViewport=None, args=['--no-sandbox'], handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False)


Then loop.run_until_complete just doesn't complete and hangs.
What to do with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Grachunov, 2021-07-21
@Qwentor

In short, I decided through multiprocessing - I wrapped it in a separate process and, accordingly, inside this process, the thread is the main one and everything works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question