I'm trying to apply certain function with two adjacent elements in given data set. Please refer to the following example.
# I'll just make a simple function here.
# In my real case, I send request to database
# to get the result with two arguments.
def get_data_from_db_with(arg1, arg2):
# write a query with arg1 and arg2 named 'query_result'
return query_result
data = [arg1, arg2, arg3, arg4]
result = []
for a, b in zip(data, data[1:]):
result.append(get_data_from_db_with(a, b))
For example if the length of data is 4 as is the case seen above, then I send request 3 times to database. Each request takes about 0.3 sec to retrieve the data, thus 0.9 second (0.3 sec * 3 requests) in total. Problem is that as the number of request increases, so does the overall time. What I want to do is, if possible, send all requests at once. Basically, it'll look like this.
With the code above,
1) get_data_from_db_with(arg1, arg2)
2) get_data_from_db_with(arg2, arg3)
3) get_data_from_db_with(arg3, arg4)
will be processed consecutively.
What I want to do, if possible, is to send requests all at once, not consecutively. Of course, the number of requests remains unchanged. But the overall time consumption will decrease based on my assumption.
Now I'm searching for async, multiprocessing and so on. Any comment or feedback would be immensely helpful.
Thanks in advance.