I am trying to implement Asynchronous support(https://docs.djangoproject.com/en/3.1/topics/async/) for Django ORM to write many records in Database(Postgres).
I am getting the ERROR:root:FATAL: remaining connection slots are reserved for non-replication superuser connections
I am creating coroutines add adding them to running the asyincio loop
# Helper Class
class ModelsHelper:
@staticmethod
@sync_to_async
def __handle_resource_data(data):
// Some calculation to create kwargs dict
return Resource.objects.get_or_create(**kwargs)
async def store_data(metric):
// some calculation to get data
return await ModelsHelper.__handle_resource_data(data)
# Main File
def get_event_loop():
loop = None
try:
loop = asyncio.get_event_loop()
except Exception as e:
print(" New Event Loop ".center(50, '*'))
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop
loop = get_event_loop()
future = asyncio.ensure_future(
asyncio.gather(*[ModelsHelper.store_data(metric) for metric in metrics]),
loop=loop
)
loop.run_until_complete(future)