I have the following three for loop (two of them are nested) in python. The API requests should be sent concurrently. How to parallelized the execution?
args = []
args.extend((n, m) for n in (1,) for m in range(1, 4))
args.extend((n, m) for n in range(2, 5) for m in range(2, 5))
args.extend((n, m) for n in range(5, 14) for m in range(1, 5))
pool = ThreadPool(len(args))
# list of packets_received:
results = pool.starmap(my_function, args)
for result in results:
# unpack the result tuple:
packets_recieved,packets_transmitted,bytes_recieved,bytes_transmitted=result
#for sw1
for m in range (1,4,1):
print(packets_recieved,packets_transmitted,bytes_recieved,bytes_transmitted)
#for sw2,sw3,sw4
for n in range (2,5,1):
for m in range (2,5,1):
print(packets_recieved,packets_transmitted,bytes_recieved,bytes_transmitted)
#for sw5 to sw13
for n in range (5,14,1):
for m in range (1,5,1):
print(packets_recieved,packets_transmitted,bytes_recieved,bytes_transmitted)
Looking at the three instances of apiString
and rewriting them to use the more succinct F-strings , they all appear to be of the form:
apiString = f'http://192.168.74.134:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:{n}/node-connector/openflow:{n}:{m}'
Even the first loop fits this format if n
is 1 and m
takes on values 1, 2, 3 and 4. So let's modify my_function
to take two arguments, n and m :
def my_function(n, m):
apiString = f'http://192.168.74.134:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:{n}/node-connector/openflow:{n}:{m}'
... # //etc.
return packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted
Now we just have to arrange to call this concurrently with the following (n, m) argument pairs, specified here as tuples (for the time being):
args = []
args.extend((n, m) for n in (1,) for m in range(1, 4))
args.extend((n, m) for n in range(2, 5) for m in range(2, 5))
args.extend((n, m) for n in range(5, 14) for m in range(1, 5))
And putting it all together:
from multiprocessing.pool import ThreadPool
def my_function(n, m):
apiString = f'http://192.168.74.134:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:{n}/node-connector/openflow:{n}:{m}'
... # //etc.
return packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted
args = []
args.extend((n, m) for n in (1,) for m in range(1, 4))
args.extend((n, m) for n in range(2, 5) for m in range(2, 5))
args.extend((n, m) for n in range(5, 14) for m in range(1, 5))
# Creating thread pool whose size is the number of requests we need to make:
# (but it could be smaller, for example 10, if you wanted less concurrency):
pool = ThreadPool(len(args))
# list of packets_received:
results = pool.starmap(my_function, args)
for result in results:
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
print(packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
Method starmap
will unpack each tuple in args
and pass the elements of the tuple as individual arguments to my_function
.
Update
I offer 3 ways of processing the results
returned by the call to starmap
. Chose one of these methods (but not all):
# Option 1: Retrieve and print all N results corresponding to the N arguments in args
for result in results:
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
print(packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
# Option 2: Retrieve and print all N results in 3 separate loops:
it = iter(results) # create an iterator for results
for n in (1,):
for m in range(1, 4):
result = next(it) # get next result
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
# you can now also print the n and m value:
print(n, m, packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
for n in (2, 5):
for m in range(2, 5):
result = next(it) # get next result
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
# you can now also print the n and m values:
print(n, m, packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
for n in (5, 14):
for m in range(1, 5):
result = next(it) # get next result
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
# you can now also print the n and m value:
print(n, m, packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
# Option 3: Using enumerate:
for idx, result in enumerate(results):
arg = args[idx]
# unpack the arg tuplet
n, m = arg
# unpack the result tuple:
packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted = result
# you can now also print the n and m values:
print(n, m, packets_recieved, packets_transmitted, bytes_recieved, bytes_transmitted)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.