Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiprocessing #5

Open
FranciscoPalomares opened this issue Apr 22, 2023 · 1 comment
Open

Multiprocessing #5

FranciscoPalomares opened this issue Apr 22, 2023 · 1 comment

Comments

@FranciscoPalomares
Copy link

Hi,
Example multiprocessing in python? I only see in one port.
Thanks

@deaktomi
Copy link

To run several browsers simultaneously you simply need to follow the answer on this stack over answer.
So simply put you code a function and start it on different processes. Something like this:

def run_browser_code(): 
    # Put your browser-related code here 
    # Search Chrome Base Profiles 
    base_profiles = client.search_base_profiles( 
        device_type='desktop', 
        browser_product='chrome' 
    ) 

    # Create a new profile with recommended settings 
    # Choose one of the Base Profiles 
    create_profile_request = BuilderForCreateProfile \ 
        .for_base_profile(base_profiles[0].id) \ 
        .set_recommended_defaults() \ 
        .build() 
    profile = client.create_profile(body=create_profile_request) 

    # Start the browser profile 
    client.start_profile(profile.id) 

    # Connect to the browser with Playwright through CDP 
    browser_ws_endpoint = f'ws://localhost:{kameleo_port}/playwright/{profile.id}' 
    with sync_playwright() as playwright: 
        browser = playwright.chromium.connect_over_cdp(endpoint_url=browser_ws_endpoint) 
        context = browser.contexts[0] 
        page = context.new_page() 

        # Use any Playwright command to drive the browser 
        # and enjoy full protection from bot detection products 
        page.goto('https://google.com') 
        page.click('div[aria-modal="true"][tabindex="0"] button + button') 
        page.click('[name=q]') 
        page.keyboard.type('Kameleo') 
        page.keyboard.press('Enter') 

    # Wait for 5 seconds 
    time.sleep(5) 

    # Stop the browser by stopping the Kameleo profile 
    client.stop_profile(profile.id) 

def func1(): 
    print("func1: starting") 
    run_browser_code() 
    print("func1: finishing") 

def func2(): 
    print("func2: starting") 
    run_browser_code() 
    print("func2: finishing") 

if __name__ == "__main__": 
    client = Client()  # Initialize your Kameleo client here 
    kameleo_port = 12345  # Replace with your actual Kameleo port 

    p1 = Process(target=func1) 
    p1.start() 
    p2 = Process(target=func2) 
    p2.start() 
    p1.join() 
    p2.join()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants