Error while opening multiple webdrivers in multithreading in python

I am using python to create web clips. I am trying to use multithreading to speed up cleanup. And I will use selenium. So, in each thread, I open one webdriver. When I open 4 streams, the program goes well. However, when I try to open 5 threads or more than 5 threads, the program returns an error like this:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\apogne\\appdata\\local\\temp\\tmpqzhfxq.webdriver.xpi\\platform\\WINNT_x86-msvc\\components\\webdriver-firefox-latest.dll'

      

The program can be simplified as follows and the same error still occurs.

from selenium import webdriver
from threading import Thread

def f():    
    driver=webdriver.Firefox()    
    driver.close()

thread_list=[]

for i in range(5):
    t=Thread(target=f)    
    t.start()    
    thread_list.append(t)

for t in thread_list:    
    t.join()

      

The complete trace of the error is as follows.

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\pinterest\user_info_multiThread.py", line 21, in gettingUserInfo
    driver = webdriver.Firefox()
  File "C:\Python27\lib\selenium\webdriver\firefox\webdriver.py", line 59, in __init__
    self.binary, timeout),
  File "C:\Python27\lib\selenium\webdriver\firefox\extension_connection.py", line 45, in __init__
    self.profile.add_extension()
  File "C:\Python27\lib\selenium\webdriver\firefox\firefox_profile.py", line 92, in add_extension
    self._install_extension(extension)
  File "C:\Python27\lib\selenium\webdriver\firefox\firefox_profile.py", line 285, in _install_extension
    shutil.rmtree(tmpdir)
  File "C:\Python27\lib\shutil.py", line 247, in rmtree
    rmtree(fullname, ignore_errors, onerror)
  File "C:\Python27\lib\shutil.py", line 247, in rmtree
    rmtree(fullname, ignore_errors, onerror)
  File "C:\Python27\lib\shutil.py", line 247, in rmtree
    rmtree(fullname, ignore_errors, onerror)
  File "C:\Python27\lib\shutil.py", line 252, in rmtree
    onerror(os.remove, fullname, sys.exc_info())
  File "C:\Python27\lib\shutil.py", line 250, in rmtree
    os.remove(fullname)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\apogne\\appdata\\local\\temp\\tmpadxbvj.webdriver.xpi\\platform\\WINNT_x86-msvc\\components\\webdriver-firefox-previous.dll'

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\pinterest\user_info_multiThread.py", line 21, in gettingUserInfo
    driver = webdriver.Firefox()
  File "C:\Python27\lib\selenium\webdriver\firefox\webdriver.py", line 61, in __init__
    keep_alive=True)
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 73, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 121, in start_session
    'desiredCapabilities': desired_capabilities,
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 173, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 166, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'c is null' ; Stacktrace: 
    at nsCommandProcessor.prototype.newSession (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/command-processor.js:11751:61)
    at nsCommandProcessor.prototype.execute (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/command-processor.js:11646:7)
    at Dispatcher.executeAs/< (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/driver-component.js:8430:5)
    at Resource.prototype.handle (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/driver-component.js:8577:219)
    at Dispatcher.prototype.dispatch (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/driver-component.js:8524:36)
    at WebDriverServer/<.handle (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/driver-component.js:11466:5)
    at createHandlerFunc/< (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:1935:41)
    at ServerHandler.prototype.handleResponse (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:2261:15)
    at Connection.prototype.process (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:1168:5)
    at RequestReader.prototype._handleResponse (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:1616:5)
    at RequestReader.prototype._processBody (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:1464:9)
    at RequestReader.prototype.onInputStreamReady (file:///c:/users/apogne/appdata/local/temp/tmpr0rxvj/extensions/fxdriver@googlecode.com/components/httpd.js:1333:9) 

Exception in thread Thread-4:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\pinterest\user_info_multiThread.py", line 24, in gettingUserInfo
    driver.get("http://www.pinterest.com")
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 185, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 171, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 380, in _request
    resp = self._conn.getresponse()
  File "C:\Python27\lib\httplib.py", line 1067, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 373, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\pinterest\user_info_multiThread.py", line 24, in gettingUserInfo
    driver.get("http://www.pinterest.com")
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 185, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 171, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 380, in _request
    resp = self._conn.getresponse()
  File "C:\Python27\lib\httplib.py", line 1067, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 373, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

Exception in thread Thread-5:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Python27\pinterest\user_info_multiThread.py", line 24, in gettingUserInfo
    driver.get("http://www.pinterest.com")
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 185, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 171, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 349, in execute
    return self._request(command_info[0], url, body=data)
  File "C:\Python27\lib\selenium\webdriver\remote\remote_connection.py", line 380, in _request
    resp = self._conn.getresponse()
  File "C:\Python27\lib\httplib.py", line 1067, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 373, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

      

Does anyone know why this error occurs and how to fix it?

+3


source to share


1 answer


You must create a lock for

driver=webdriver.Firefox()

      

so only one thread at a time loads the driver



change:

from selenium import webdriver
from threading import Thread, Lock

def f():    
    #thread will either acquire lock or wait for it to be released by other thread
    with my_lock:
        #init this driver
        driver = webdriver.Firefox()    

    #do your other stuff

    driver.close()

thread_list=[]
my_lock = Lock()

for _ in xrange(5):
    t = Thread( target=f )    
    t.start()    
    thread_list.append( t )

for t in thread_list:    
    t.join()

      

0


source







All Articles