runware 0.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
runware/__init__.py ADDED
@@ -0,0 +1,10 @@
1
+ from .server import RunwareServer as Runware
2
+ from .types import IRemoveImageBackground
3
+ from .types import *
4
+ from .utils import *
5
+ from .base import *
6
+ from .logging_config import *
7
+ from .async_retry import *
8
+
9
+ __all__ = ["Runware", "IRemoveImageBackground"]
10
+ __version__ = "0.1.0"
runware/async_retry.py ADDED
@@ -0,0 +1,96 @@
1
+ import asyncio
2
+
3
+
4
+ async def asyncRetry(apiCall, options=None):
5
+ """
6
+ Retry an asynchronous API call multiple times with configurable options.
7
+
8
+ :param apiCall: The asynchronous function to be retried.
9
+ :param options: An optional dictionary that allows you to configure the retry behavior.
10
+ It has the following properties:
11
+ - maxRetries: The maximum number of retries before giving up (default is 1).
12
+ - delayInSeconds: The delay in seconds between each retry attempt (default is 1).
13
+ - callback: A function that will be called after each failed attempt.
14
+ :return: The result of the successful API call.
15
+
16
+ This function retries an asynchronous API call multiple times with configurable options.
17
+ It attempts to execute the `apiCall` and returns the result if successful. If the `apiCall`
18
+ raises an exception, it calls the `callback` function (if provided), introduces a delay
19
+ before the next retry attempt, and continues retrying until the maximum number of retries
20
+ is reached. If all retry attempts are exhausted and the `apiCall` still fails, it raises
21
+ the last encountered exception.
22
+
23
+ Example:
24
+ async def myApiCall():
25
+ # API call logic here
26
+ ...
27
+
28
+ result = await asyncRetry(myApiCall, options={
29
+ 'maxRetries': 3,
30
+ 'delayInSeconds': 1,
31
+ 'callback': lambda: print('Retry attempt failed')
32
+ })
33
+ print(result)
34
+ """
35
+ if options is None:
36
+ options = {}
37
+ delayInSeconds = options.get("delayInSeconds", 1)
38
+ callback = options.get("callback")
39
+ maxRetries = options.get("maxRetries", 1)
40
+
41
+ for attempt in range(maxRetries):
42
+ try:
43
+ return await apiCall()
44
+ except Exception as error:
45
+ if callback:
46
+ callback()
47
+ if attempt < maxRetries - 1:
48
+ await asyncio.sleep(delayInSeconds)
49
+ else:
50
+ raise error
51
+
52
+
53
+ async def asyncRetryGather(apiCalls, options=None):
54
+ """
55
+ Retry multiple asynchronous API calls concurrently with configurable options.
56
+
57
+ :param apiCalls: A list of asynchronous functions to be retried.
58
+ :param options: An optional dictionary that allows you to configure the retry behavior.
59
+ It has the following properties:
60
+ - maxRetries: The maximum number of retries before giving up (default is 1).
61
+ - delayInSeconds: The delay in seconds between each retry attempt (default is 1).
62
+ - callback: A function that will be called after each failed attempt.
63
+ :return: A list of results from the successful API calls.
64
+
65
+ This function retries multiple asynchronous API calls concurrently with configurable options.
66
+ It creates tasks for each `apiCall` and executes them concurrently using `asyncio.gather()`.
67
+ Each task represents the execution of `asyncRetry` for a single `apiCall`. The results of
68
+ each successful API call are returned as a list in the same order as the input `apiCalls`.
69
+
70
+ Example:
71
+ async def myApiCall1():
72
+ # API call logic here
73
+ ...
74
+
75
+ async def myApiCall2():
76
+ # API call logic here
77
+ ...
78
+
79
+ results = await asyncRetryGather([myApiCall1, myApiCall2], options={
80
+ 'maxRetries': 3,
81
+ 'delayInSeconds': 1,
82
+ 'callback': lambda: print('Retry attempt failed')
83
+ })
84
+ print(results)
85
+ """
86
+ if options is None:
87
+ options = {}
88
+ delayInSeconds = options.get("delayInSeconds", 1)
89
+ callback = options.get("callback")
90
+ maxRetries = options.get("maxRetries", 1)
91
+
92
+ tasks = []
93
+ for apiCall in apiCalls:
94
+ task = asyncio.create_task(asyncRetry(apiCall, options))
95
+ tasks.append(task)
96
+ return await asyncio.gather(*tasks)