question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to process 2 pages in diffrent threads?

See original GitHub issue

i want to open 2 pages and create 2 thread,thread 1 process page1,and thread 2 process page 2. i try this code

def run1(context):
    page = context.new_page()
    page.goto('https://page1')
    page.wait_for_timeout(5000)
    page.close()

def run2(context):
    page = context.new_page()
    page.goto('https://page2')
    page.wait_for_timeout(1000)
    page.close()

def main():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        context = browser.new_context()
        t=Thread(target=run1,args=(context,))
        t1=Thread(target=run2,args=(context,))
        t.start()
        t1.start()
        t.join()
        t1.join()
        context.close()
        browser.close()

but first open page 1 and 5 seconds later ,it opens page 2. it oepens the pages one by one how can i process multi page in diffrent thread at same time

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
kumaraditya303commented, Apr 16, 2021

Playwright isn’t thread safe so you need to start playwright separately for each thread or you can use asyncio

from playwright.sync_api import sync_playwright
from threading import Thread


def run1():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        page = browser.new_page()
        page.goto("https://google.com")
        page.wait_for_timeout(1000)
        page.close()


def run2():
    with sync_playwright() as playwright:
        browser = playwright.chromium.launch(headless=False)
        page = browser.new_page()
        page.goto("https://google.com")
        page.wait_for_timeout(1000)
        page.close()


def main():
    t = Thread(target=run1)
    t1 = Thread(target=run2)
    t.start()
    t1.start()
    t.join()
    t1.join()


if __name__ == "__main__":
    main()
1reaction
simplPartcommented, Jun 12, 2021

combine threading and asyncio

can you show how to do it correctly with asyncio? and does firefox have any arguments like https://stackoverflow.com/a/58589026 to reduce the load on the processors? (I have many contexts running. 1 browser per process and many contexts per thread).

first I start the process -> start the asyncio loop in the process -> start the browser -> create about 5 threads, transfer the browser -> start about 5 contexts per thread. - This loads the server heavily (6 cores, 32 RAM), cores are loaded at 100% and then the browser is simply closed and “Target page, context or browser has been closed” ((

Read more comments on GitHub >

github_iconTop Results From Across the Web

What resources are shared between threads? - Stack Overflow
The process model is based on two independent concepts: resource grouping and execution. Sometimes it is useful to separate them; this is where ......
Read more >
How web browsers use Processes and Threads
If two processes need to talk, they can do so by using Inter Process Communication (IPC). Many applications are designed to work this...
Read more >
Threads
Memory map with two threads. A process may be multithreaded, where the same program contains multiple concurrent threads of execution.
Read more >
Threads vs. Processes: A Look At How They Work Within Your ...
In some cases, you'll see a process marked as “Not Responding.” Try quitting that process and see if your system runs better. If...
Read more >
Reactive Multi-Threading with RxJava - Pitfalls and Solutions
... the coordinator thread loads pages of 3 messages at a time and forwards them to a thread pool of 2 worker threads...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found