[Python] Wanting to optimize a python playwright proxy applicatoin

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm writing a Python Flask-based proxy using the playwright library. I've got code that works, but I'd like to make it faster. Here's what I have:

    @app.route('/fetch/')
    def fetch_url():
    url = request.args.get('url')
    return fetch(url)


    def fetch (url):
    print("Starting")
    p = sync_playwright().start()
    browser = p.firefox.launch()

    page = browser.new_page()
    page.goto(url)
    page.wait_for_url(url)

    body = page.content()
    print(page.title())
    page.close()

    return body


    I would like to only incur the cost of:

    p = sync_playwright().start()
    browser = p.firefox.launch()


    during start-up and simply reuse the p or browser objects for each subsequent request.

    However, when I move those lines to the top of my program, I get this error message when I send a request to my proxy:

    greenlet.error: Cannot switch to a different thread Current: <greenlet.greenlet object at 0x79b0d4b22f80 (otid=0x79b0d4b02e20) current active started main> Expected: <greenlet.greenlet object at 0x79b0d4fcbb80 (otid=0x79b0d57e9d70) suspended active started main>

    Any pointers to what I need to do would be MOST welcome.

    Mike.

    Continue reading...

Compartilhe esta Página