Python version: 3.5.2
I'm trying to implement an easy to use interface using asyncIO for my end users.
In my model, the end user defines an async coroutine that operates on singular object(s).
e.g., user_func(addr_object, property_object)
Now, I would like to do something like this...
User Code:
calling this file "user.py"
# instantiating my_api object
batch_runner = my_api.BatchRunner()
# coro_list can have more than one user defined function
coro_list = [user_func(addr_object, property_object)]
# this method takes care of creating the eventloop and running it
batch_runnner.run(coro_list)
# all user defined coroutines live in this file "user.py"
async def user_func(addr_object, property_object):
``` operate on the objects
# await on some operation
P.S.: addr_object and property_object can be assigned to None in user.py to keep Python happy
My API code:
# calling this file "my_api.py"
class BatchRunner(object):
...
def run(self, coro_list):
new_coro_list = list()
# user can define multiple coroutines, but same type and length of input arguments on all of them
for coros in coro_list:
# Say I've 10 different addr_objects for example
for addr_obj in addr_obj_list:
# and say another 5 property_objects
for prop_obj in prop_obj_list:
# how to modify coro_list to work on each of these addr_obj and prop_obj?
# while maintaining reference to the user defined coroutine in user.py?
new_coro_list.append(coros(addr_obj, prop_obj)
eventloop = asyncio.get_event_loop()
eventloop.run_until_complete(new_coro_list)
My questions are:
- Is it possible to maintain reference to a coroutine object declared in another file (user.py) and modify the value of its input arguments before actual eventloop execution inside of my_api.py? i.e., same function call, but with modified argument values.
- If that is not possible, is there a better way to hide the iteration logic under the hood? (Could be using **kwargs with user function name being passed as a string)