
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Opnieuw is a general-purpose retrying library, written in Python, in order to simplify the task of adding retry behavior to both synchronous as well as asynchronous tasks. Opnieuw is easy and straightforward to use.
Opnieuw makes it easy to add robust retries:
See our announcement post for a bit more background on why we wrote Opnieuw.
Suppose we want to parse https://tech.channable.com/atom.xml
and we want to
add a retry to handle a specific network Exception. We can add Opnieuw to our
network handler as follows:
import requests
from requests.exceptions import ConnectionError, HTTPError
from opnieuw import retry
@retry(
retry_on_exceptions=(ConnectionError, HTTPError),
max_calls_total=4,
retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
response = requests.get('https://tech.channable.com/atom.xml')
return response.text
retry_on_exceptions
specifies which exceptions we want to retry on.
max_calls_total
is the maximum number of times that the decorated function
gets called, including the initial call.
retry_window_after_first_call_in_seconds
is the maximum number of seconds
after the first call was initiated, where we would still do a new attempt.
To install Opnieuw, simply:
$ pip install opnieuw
The short example above provides a concise demonstration of how Opnieuw could
be used. Let's dig deeper into Opnieuw and add another exception to
retry_on_exceptions
to do a retry on:
from urllib.error import URLError
import requests
from opnieuw import RetryException, retry
@retry(
retry_on_exceptions=(ConnectionError, RetryException, URLError),
max_calls_total=4,
retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
response = requests.get('https://tech.channable.com/atom.xml')
if response.status_code != 200:
raise RetryException
return response.text
We can pass the name of exception we want do retry for or a tuple of exceptions
to the retry_on_exceptions
. As mentioned earlier Opnieuw was developed to
make it more convenient to add retry behavior to any task.
Let's make it a little bit more generic and define a list of retry exceptions that should trigger a retry of the function:
STANDARD_HTTP_EXCEPTIONS = (
ConnectionError,
ProtocolError,
RetryException,
...
)
@retry(
retry_on_exceptions=STANDARD_HTTP_EXCEPTIONS,
max_calls_total=4,
retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
response = requests.post('https://tech.channable.com/atom.xml')
return response.text
Now our retry is more generic, as exceptions raised which are in
STANDARD_HTTP_EXCEPTIONS
will be retried.
If you want retry behavior for async tasks, then there is also an async retry which basically work the same way, but for async tasks.
Here is the example above but in async mode:
from opnieuw import retry_async
STANDARD_HTTP_EXCEPTIONS = (
ConnectionError,
EOFError,
RetryException,
...
)
@retry_async(
retry_on_exceptions=STANDARD_HTTP_EXCEPTIONS,
max_calls_total=4,
retry_window_after_first_call_in_seconds=60,
)
async def get_page() -> str:
response = requests.get('https://tech.channable.com/atom.xml')
return response.text
FAQs
Retries for humans
We found that opnieuw demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.