PrivateUser
PrivateUser

Reputation: 4524

Python - How to capture gevent socket timeout exception

import gevent.monkey
gevent.monkey.patch_socket()
import requests
from gevent.pool import Pool
import socket

urls = ["http://www.iraniansingles.com"]


def check_urls(urls):
    pool = Pool(1)
    for url in urls:
        pool.spawn(fetch, url)
    pool.join()

def fetch(url):
    print url
    try:
        resp = requests.get(url, verify=False, timeout=5.0)
        print resp.status_code
    except socket.timeout:
        print "SocketTimeout"

check_urls(urls)

If I remove the first 2 lines, my program printing SocketTimeout. But with monkeypatch, my program waits forever.

Can someone tell me how to capture that socket timeout exception with monkeypatch?

Upvotes: 3

Views: 1303

Answers (1)

PrivateUser
PrivateUser

Reputation: 4524

Problem was gevent default timeout set to None. So we have to set default socket timeout manually.

from gevent import socket
socket.setdefaulttimeout(5)

Upvotes: 3

Related Questions