I have been working with a very simple bit of code, but the behavior is very strange. I am trying to send a request to a webpage using requests.get
, but if the request takes longer than a few seconds, I would like to kill the process. I am following the response from the accepted answer here, but changing the function body to include the request
. My code is below:
import multiprocessing as mp, requests
def get_page(_r):
_rs = requests.get('https://www.woolworths.com.au/shop/browse/drinks/cordials-juices-iced-teas/iced-teas').text
_r.put(_rs)
q = mp.Queue()
p = mp.Process(target=get_page, args=(q,))
p.start()
time.sleep(3)
p.terminate()
p.join()
try:
result = q.get(False)
print(result)
except:
print('failed')
The code above simply hanges when running it. However, when I run
requests.get('https://www.woolworths.com.au/shop/browse/drinks/cordials-juices-iced-teas/iced-teas').text
independently, a response is returned in under two seconds. Therefore, main code should print the page's HTML, however, it just stalls. Oddly, when I put an infinite loop in get_page
:
def get_page(_r):
while True:
pass
_r.put('You will not see this')
the process is indeed terminated after three seconds. Therefore, I am certain the behavior has to do with requests
. How could this be? I discovered a similar question here, but I am not using async
. Could the issue have to do with monkey patching, as I am using requests
along with time
and multiprocessing
? Any suggestions or comments would be appreciated. Thank you!
I am using:
Python 3.7.0
requests
2.21.0
Edit: @Hitobat pointed out that a param timeout
can be used instead with requests
. This does indeed work, however, I would appreciate any other ideas pertaining to why the requests
is failing with multiprocessing
.