Skip to content

Commit b321e59

Browse files
committed
Added timeout to rest_grouping.py
1 parent d6c6d4b commit b321e59

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

README.rst

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,31 +421,37 @@ Sample User Apps/Modules/Scripts
421421
"[+++]": [
422422
{
423423
"(://)": "http://service1",
424-
"(...)": {"svc1-arg1": "arg1 of service1 payload ..." }
424+
"(...)": {"svc1-arg1": "arg1 of service1 payload ..." },
425+
"(:/!)": 600
425426
},
426427
{
427428
"(://)": "http://service2",
428-
"(.|.)": {"svc2-arg1": "arg1 of service2 payload ..." }
429+
"(.|.)": {"svc2-arg1": "arg1 of service2 payload ..." },
430+
"(:/!)": 600
429431
},
430432
{
431433
"[###]": [
432434
{
433435
"(://)": "http://service3",
434-
"(...)": {"svc3-arg1": "arg1 of service3 payload ..." }
436+
"(...)": {"svc3-arg1": "arg1 of service3 payload ..." },
437+
"(:/!)": 1800
435438
},
436439
{
437440
"(://)": "http://service4",
438-
"(...)": {"svc4-arg1": "arg1 of service4 payload ..." }
441+
"(...)": {"svc4-arg1": "arg1 of service4 payload ..." },
442+
"(:/!)": 1800
439443
},
440444
{
441445
"(://)": "http://service5",
442-
"(...)": {"svc5-arg1": "arg1 of service5 payload ..." }
446+
"(...)": {"svc5-arg1": "arg1 of service5 payload ..." },
447+
"(:/!)": 1800
443448
}
444449
]
445450
},
446451
{
447452
"(://)": "http://service6",
448-
"(...)": {"svc6-arg1": "arg1 of service6 payload ..." }
453+
"(...)": {"svc6-arg1": "arg1 of service6 payload ..." },
454+
"(:/!)": 600
449455
}
450456
]
451457
}
@@ -459,13 +465,14 @@ Sample User Apps/Modules/Scripts
459465

460466
#. Single Service (Leaf Service)
461467

462-
This is the most basic unit that constitutes a service group (virtual service). It requires a URL and a dictionary of arguments as the payload:
468+
This is the most basic unit that constitutes a service group (virtual service). It requires a URL, a dictionary of arguments as the payload, and an optional timeout seconds:
463469

464470
.. code-block:: JSON
465471
466472
{
467473
"(://)": "http://service1",
468-
"(...)": {"svc1-arg1": "arg1 of service1 payload ..." }
474+
"(...)": {"svc1-arg1": "arg1 of service1 payload ..." },
475+
"(:/!)": timeout seconds
469476
}
470477
471478
Or
@@ -474,12 +481,14 @@ Sample User Apps/Modules/Scripts
474481
475482
{
476483
"(://)": "http://service2",
477-
"(.|.)": {"svc2-arg1": "arg1 of service2 payload ..." }
484+
"(.|.)": {"svc2-arg1": "arg1 of service2 payload ..." },
485+
"(:/!)": timeout seconds
478486
}
479487
480-
+ "``(://)``" - *Key : Value* - URL of the service call
481-
+ "``(...)``" - *Key : Value* - a dictionary of arguments (payload) for the service call
482-
+ "``(.|.)``" - *Key : Value* - merge the results of the previous service as pipeline arguments into the current arguments
488+
+ "``(://)``" - *Key : Value* - "URL of the service call"
489+
+ "``(...)``" - *Key : Value* - {A dictionary of arguments (payload) for the service call}
490+
+ "``(.|.)``" - *Key : Value* - {Merge the results of the previous service as pipeline arguments into this dictionary of arguments}
491+
+ "``(:/!)``" - *Key : Value* - timeout seconds (optional) How many seconds to wait for the REST service to respond before giving up
483492

484493
Each service is an executable/callable unit, let's have a convention to use a rounded rectangle as its graphical symbol.
485494

Sample/UserApps/ServicesGrouping/rest_grouping.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
from simple_rest_call import request_json
1818

1919

20-
_reserved_key_parallel_group : str = '[###]'
21-
_reserved_key_serial_group : str = '[+++]'
22-
_reserved_key_rest_url : str = '(://)'
23-
_reserved_key_payload : str = '(...)'
24-
_reserved_key_payload_with_pipe : str = '(.|.)'
20+
_reserved_key_parallel_group : str = "[###]"
21+
_reserved_key_serial_group : str = "[+++]"
22+
_reserved_key_rest_url : str = "(://)"
23+
_reserved_key_payload : str = "(...)"
24+
_reserved_key_payload_with_pipe : str = "(.|.)"
25+
_reserved_key_timeout : str = "(:/!)"
26+
27+
28+
def _task_func(url:str, data:dict=None, timeout:float=None):
29+
return request_json(url, data, timeout=timeout)
2530

2631

2732
def _pipeargs_merge_fn(kw_args:Dict[str, Any], pipe_args:Dict[str, Any]) -> Dict[str, Any]:
@@ -43,13 +48,10 @@ class RestTaskLoader(ITaskLoader):
4348
"""This class is used to load a group of RESTful services (call tasks) from a JSON payload into ``TaskContainer``"""
4449
def __init__(self, thread_pool:ThreadPoolExecutor):
4550
self.thread_pool = thread_pool
46-
self.func = request_json
47-
self.merge_fn = _pipeargs_merge_fn
48-
self.timeout = None
4951

5052

5153
def create_base_container(self) -> TaskContainer:
52-
return TaskContainer(self.func, self.merge_fn, self.thread_pool, **{'timeout':self.timeout})
54+
return TaskContainer(_task_func, _pipeargs_merge_fn, self.thread_pool)
5355

5456

5557
def extract_single_task(self, task_node:Dict[str, Any]) -> Tuple[tuple, Dict[str, Any], bool]:
@@ -67,7 +69,9 @@ def extract_single_task(self, task_node:Dict[str, Any]) -> Tuple[tuple, Dict[str
6769
else:
6870
with_pipe = False
6971

70-
return ((), {'url': url, 'data': data}, with_pipe)
72+
timeout = task_node.get(_reserved_key_timeout)
73+
74+
return ((), {'url': url, 'data': data, 'timeout': timeout}, with_pipe)
7175
else:
7276
return None
7377

@@ -80,10 +84,15 @@ def extract_parallel_group(self, task_node:Dict[str, Any]) -> List[Dict[str, Any
8084
return task_node.get(_reserved_key_parallel_group)
8185

8286

87+
def load(self, task_tree:Dict[str, Any]) -> TaskContainer:
88+
container = super().load(task_tree)
89+
container.timeout = task_tree.get(_reserved_key_timeout)
90+
return container
91+
8392

8493
def start(rest:Dict[str, Any]):
8594
"""the main entry for the client to call a group of RESTful services."""
86-
with ThreadPoolExecutor(max_workers=32) as thread_pool:
95+
with ThreadPoolExecutor(max_workers=64) as thread_pool:
8796
loader = RestTaskLoader(thread_pool)
8897
container = loader.load(rest)
8998
return container.run()

Utilities.PyPI/task_grouping.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
1515
This is an abstract base class for implementing a concrete loader class to load a task tree from Dict/JSON to ``TaskContainer``.
1616
17+
For detailed usage, see the practice sample code: https://github.com/DataBooster/PyWebApi/blob/master/Sample/UserApps/ServicesGrouping/rest_grouping.py
18+
19+
and its product documentation: https://github.com/DataBooster/PyWebApi#services-grouping
20+
1721
----
1822
1923
| Homepage and documentation: https://github.com/DataBooster/PyWebApi
@@ -290,4 +294,4 @@ def load(self, task_tree:Dict[str, Any]) -> TaskContainer:
290294

291295

292296

293-
__version__ = "0.1a1"
297+
__version__ = "0.1a2"

0 commit comments

Comments
 (0)