Connection pool is full, discarding connection with ThreadPoolExecutor and multiple headless browsers through Selenium and Python

后端 未结 2 1228
我寻月下人不归
我寻月下人不归 2021-01-20 03:58

I\'m writing some automation software using selenium==3.141.0, python 3.6.7, chromedriver 2.44.

Most of the the logic is ok to

相关标签:
2条回答
  • 2021-01-20 04:46

    This error message...

    WARNING|05/Dec/2018 17:33:11|connectionpool|_put_conn|274|Connection pool is full, discarding connection: 127.0.0.1
    WARNING|05/Dec/2018 17:33:11|connectionpool|urlopen|662|Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))': /session/119df5b95710793a0421c13ec3a83847/url
    WARNING|05/Dec/2018 17:33:11|connectionpool|urlopen|662|Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fcee7ada048>: Failed to establish a new connection: [Errno 111] Connection refused',)': /session/119df5b95710793a0421c13ec3a83847/url
    

    ...seems to be an issue in urllib3's connection pooling which raised these WARNING while executing the def _put_conn(self, conn) method in connectionpool.py.

    def _put_conn(self, conn):
        """
        Put a connection back into the pool.
    
        :param conn:
            Connection object for the current host and port as returned by
            :meth:`._new_conn` or :meth:`._get_conn`.
    
        If the pool is already full, the connection is closed and discarded
        because we exceeded maxsize. If connections are discarded frequently,
        then maxsize should be increased.
    
        If the pool is closed, then the connection will be closed and discarded.
        """
        try:
            self.pool.put(conn, block=False)
            return  # Everything is dandy, done.
        except AttributeError:
            # self.pool is None.
            pass
        except queue.Full:
            # This should never happen if self.block == True
            log.warning(
                "Connection pool is full, discarding connection: %s",
                self.host)
    
        # Connection never got put back into the pool, close it.
        if conn:
            conn.close()
    

    ThreadPoolExecutor

    ThreadPoolExecutor is an Executor subclass that uses a pool of threads to execute calls asynchronously. Deadlocks can occur when the callable associated with a Future waits on the results of another Future.

    class concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())
    
    • An Executor subclass that uses a pool of at most max_workers threads to execute calls asynchronously.
    • initializer is an optional callable that is called at the start of each worker thread; initargs is a tuple of arguments passed to the initializer. Should initializer raise an exception, all currently pending jobs will raise a BrokenThreadPool, as well as any attempt to submit more jobs to the pool.
    • From version 3.5 onwards: If max_workers is None or not given, it will default to the number of processors on the machine, multiplied by 5, assuming that ThreadPoolExecutor is often used to overlap I/O instead of CPU work and the number of workers should be higher than the number of workers for ProcessPoolExecutor.
    • From version 3.6 onwards: The thread_name_prefix argument was added to allow users to control the threading.Thread names for worker threads created by the pool for easier debugging.
    • From version 3.7: Added the initializer and initargs arguments.

    As per your question as you are trying to launch 10-20 instances the default connection pool size of 10 seems not to be enough in your case which is hardcoded in adapters.py.

    Moreover, @EdLeafe in the discussion Getting error: Connection pool is full, discarding connection mentions:

    It looks like within the requests code, None objects are normal. If _get_conn() gets None from the pool, it simply creates a new connection. It seems odd, though, that it should start with all those None objects, and that _put_conn() isn't smart enough to replace None with the connection.

    However the merge Add pool size parameter to client constructor have fixed this issue.

    Solution

    Increasing the default connection pool size of 10 which was earlier hardcoded in adapters.py and now configurable will solve your issue.


    Update

    As per your comment update ...submit a single instance and the outcome is the same.... as per @meferguson84 within the discussion Getting error: Connection pool is full, discarding connection:

    I stepped into the code to the point where it mounts the adapter just to play with the pool size and see if it made a difference. What I found was that the queue is full of NoneType objects with the actual upload connection being the last item in the list. The list is 10 items long (which makes sense). What doesn't make sense is that the unfinished_tasks parameter for the pool is 11. How can this be when the queue itself is only 11 items? Also, is it normal for the queue to be full of NoneType objects with the connection we are using being the last item on the list?

    That sounds like a possible cause in your usecase as well. It may sound redundant but you may still perform a couple of ad-hoc steps as follows:

    • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
    • (WindowsOS only) Use CCleaner tool to wipe off all the OS chores before and after the execution of your Test Suite.
    • (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint before and after the execution of your Test Suite.
    0 讨论(0)
  • 2021-01-20 04:55

    please see your error

    ProtocolError('Connection aborted.', 
      RemoteDisconnected('Remote end closed connection without response',))
    
    'NewConnectionError('<urllib3.connection.HTTPConnection object at >: 
       Failed to establish a new connection: [Errno 111] Connection refused',)':
    

    The error come because you are doing multiple connection too fast, it can be server down or server block your request.

    0 讨论(0)
提交回复
热议问题