This repository has been archived by the owner on Dec 9, 2019. It is now read-only.
forked from Jiramew/webdriver_pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequestiumpool.py
110 lines (92 loc) · 3.79 KB
/
requestiumpool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from requestium import Session
from datetime import datetime
from http.client import BadStatusLine
from selenium.common.exceptions import WebDriverException
class RequestiumPool():
def __init__( self, requestium_args, pool_size=4 ):
'''
Arguments:
pool_size - int - the number of requestium sessions you want open at any
given point in time. if all are being used, you wait.
requestium_args - dict - dictionary of args for setting up requestium
unused_destroy_timeout - int - number of seconds to wait before an unused
requestium session is destroyed. Clock
resets every time the session is used.
'''
self.available = list()
self.inuse = list()
self.stopped = False
self.pool_size = pool_size
self.requestium_args = requestium_args
def acquire( self, acquire_wait_timeout=15 ):
'''
Purpose: Get a requestium session.
Arguments:
acquire_wait_timeout - int - time to wait for a requestium session to free up
Returns:
S - requestium.Session - requestium Session object
'''
S = None
# IF SESSION AVAILABLE, TAKE IT
if len( self.available ) > 0:
S = self.available.pop()
self.inuse.append( S )
# NO SESSION AVAILABLE, CREATE ONE
elif len( self.inuse ) < self.pool_size:
S = Session( **self.requestium_args )
self.inuse.append( S )
# NO SESSIONS AVAILABLE AND NO MORE ALLOWED, WAIT FOR ONE
else:
StartWaitTime = datetime.now()
while ( datetime.now() - StartWaitTime ).seconds <= acquire_wait_timeout\
and ( len( self.inuse ) + len( self.available ) ) > 0:
if len( self.available ) > 0:
S = self.available.pop()
self.inuse.append( S )
break
return S
def release( self, oSession ):
'''
Purpose: Release the requestium session back to the pool.
Clear all the cookies from requests session
and selenium driver.
Arguments:
oSession - requestium.Session - requestium Session object to release
Returns:
Nothing
'''
if oSession != None:
try:
oSession.cookies.clear() # clear all cookies from the session
oSession.driver.delete_all_cookies() # clear cookies from selenium driver
if oSession in self.inuse:
self.inuse.remove( oSession )
self.available.append( oSession )
except ( ConnectionRefusedError, BadStatusLine,
WebDriverException, ConnectionAbortedError ): # a few exceptions from release overlap
pass
return
def destroy( self, oSession ):
'''
Purpose: Destroy requestium session.
Arguments:
oSession - requestium.Session - requestium Session object to destroy
Returns:
Nothing
'''
if oSession != None:
if oSession in self.inuse:
self.inuse.remove( oSession )
if oSession in self.available:
self.available.remove( oSession )
oSession.driver.quit()
oSession = None
return
def stop(self):
'''
Purpose: Close and destroy all requestium sessions.
'''
for oSession in reversed( self.inuse ):
self.destroy( oSession )
for oSession in reversed( self.available ):
self.destroy( oSession )