jonbon
jonbon

Reputation: 1200

Python Threading with Selenium

I'm attempting to use Splinter (a package for Selenium) in multiple instances. However, instances aren't starting until the first thread completely finishes. So, the browser instance opens up, loads the page, sleeps, and only then does the 2nd thread start.

I need to run instances at the same time.

import threading
import time
from splinter import Browser


def worker(proxy, port):
    proxy_settings = {"network.proxy.type": 1,
                      "network.proxy.ssl": proxy,
                      "network.proxy.ssl_port": port,
                      "network.proxy.socks": proxy,
                      "network.proxy.socks_port": port,
                      "network.proxy.socks_remote_dns": True,
                      "network.proxy.ftp": proxy,
                      "network.proxy.ftp_port": port
                      }

    browser = Browser('firefox',
                      profile_preferences=proxy_settings,
                      capabilities={'pageLoadStrategy': 'eager'}) #eager or normal
    print("Proxy: ", proxy, ":", proxy)
    browser.visit("https://mxtoolbox.com/whatismyip/?" + proxy)
    time.sleep(2)


ip1 = '22.22.222.222'
ip2 = '222.222.22.222'
p1 = int(2222)
p2 = int(2222)

p = []
p.append((ip1,p1))
p.append((ip2,p2))
x = 0
for pp in p:
    threading.Thread(target=worker(pp[0], pp[1])).start()

In my longer code (the above is my attempt to figure out why I can't multi-thread) Im also getting an error in my editor that

Local variable 'browser' value is not used

Upvotes: 0

Views: 1036

Answers (1)

Adonis
Adonis

Reputation: 4808

That's because you're not starting a thread with the worker function, actually the last line should rather look like:

threading.Thread(target=worker, args=(pp[0], pp[1])).start()

As for your editor issue, I'd say it is editor dependent and without any information, it's hard to say (I'd mention that running pylint does not indicate such warning)

Upvotes: 1

Related Questions