2323]
2424
2525COUNT_OF_OPERATIONS_PER_PROCESS = 10_000
26- COUNT_OF_PROCESSES = multiprocessing .cpu_count ()
26+ COUNT_OF_PROCESSES = 1 # multiprocessing.cpu_count()
2727
2828PERCENT_OF_WRITES = 1
2929
30- LOG_EVERY_REQUEST = False
30+ LOG_EVERY_REQUEST = True
3131
3232# =======================================
3333
@@ -47,27 +47,37 @@ def one_request(conn: http.client.HTTPConnection) -> None:
4747 'Accept' : 'application/json' ,
4848 }
4949
50- if writing :
51- new_value = f'val_{ random .randint (1000 , 1000_000_000 )} '
50+ while True : # retry connection loop
51+ try :
52+ if writing :
53+ new_value = f'val_{ random .randint (1000 , 1000_000_000 )} '
5254
53- data = {"value" : new_value }
55+ data = {"value" : new_value }
5456
55- if LOG_EVERY_REQUEST :
56- print ('====================================================' )
57- print (f'POST { url } : { data } ' )
57+ if LOG_EVERY_REQUEST :
58+ print ('====================================================' )
59+ print (f'POST { url } : { data } ' )
5860
59- request_body_bytes = json .dumps (data , indent = 2 , sort_keys = True ).encode (encoding = 'utf8' )
60- conn .request (method = 'POST' , url = url , body = request_body_bytes , headers = headers_post )
61- else :
62- if LOG_EVERY_REQUEST :
63- print (f'GET { url } ' )
64- conn .request (method = 'GET' , url = url , headers = headers_get )
61+ request_body_bytes = json .dumps (data , indent = 2 , sort_keys = True ).encode (encoding = 'utf8' )
62+ conn .request (method = 'POST' , url = url , body = request_body_bytes , headers = headers_post )
63+ else :
64+ if LOG_EVERY_REQUEST :
65+ print (f'GET { url } ' )
66+ conn .request (method = 'GET' , url = url , headers = headers_get )
6567
66- response = conn .getresponse ()
67- if LOG_EVERY_REQUEST :
68- print (f'REPLY: { response .status } { response .reason } ' )
68+ response = conn .getresponse ()
69+ if LOG_EVERY_REQUEST :
70+ print (f'REPLY: { response .status } { response .reason } ' )
6971
70- body_bytes = response .read ()
72+ body_bytes = response .read ()
73+
74+ break # exit infinite retry connection loop
75+ except ConnectionError as error :
76+ print ('Failed making HTTP request: ' , repr (error ))
77+ conn .close ()
78+ sleep_duration_sec = 5.0
79+ print (f'Retry after sleeping { sleep_duration_sec } seconds...' )
80+ time .sleep (sleep_duration_sec )
7181
7282 if response .status not in (200 , 404 ):
7383 print (f'RAW_REPLY_BODY: { body_bytes } ' )
@@ -113,10 +123,10 @@ def main() -> None:
113123 print ('main: end' )
114124
115125
116- def main_owner ( ) -> None :
117- print (f'main_owner : start { COUNT_OF_PROCESSES } child processes' )
126+ def spawn_processes ( process_count ) -> None :
127+ print (f'spawn_processes : start { process_count } child processes' )
118128
119- pool = [multiprocessing .Process (target = main , args = ()) for _ in range (COUNT_OF_PROCESSES )]
129+ pool = [multiprocessing .Process (target = main , args = ()) for _ in range (process_count )]
120130
121131 begin = time .perf_counter ()
122132
@@ -125,17 +135,20 @@ def main_owner() -> None:
125135
126136 for process in pool :
127137 process .join ()
128- print ('Child process finished' )
138+ print ('spawn_processes: Child process finished' )
129139
130140 end = time .perf_counter ()
131141 elapsed = max (end - begin , 0.001 )
132- operation_count = COUNT_OF_OPERATIONS_PER_PROCESS * COUNT_OF_PROCESSES
142+ operation_count = COUNT_OF_OPERATIONS_PER_PROCESS * process_count
133143 ops_per_sec = int (operation_count / elapsed )
134144
135- print (f'main_owner : It took { elapsed :.3f} seconds to execute { operation_count } requests; { ops_per_sec } requests per second' )
145+ print (f'spawn_processes : It took { elapsed :.3f} seconds to execute { operation_count } requests; { ops_per_sec } requests per second' )
136146
137- print ('main_owner : end' )
147+ print ('spawn_processes : end' )
138148
139149
140150if __name__ == '__main__' :
141- main_owner ()
151+ if COUNT_OF_PROCESSES == 1 :
152+ main ()
153+ else :
154+ spawn_processes (COUNT_OF_PROCESSES )
0 commit comments