Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31958 closed Bug (invalid)

Django ORM sync_to_async error FATAL: remaining connection slots are reserved for non-replication superuser connections

Reported by: Supratim Samantray Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: ORM Async
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

Change History (2)

comment:1 by Carlton Gibson, 4 years ago

Resolution: invalid
Status: newclosed

This is a usage issue. Please see TicketClosingReasons/UseSupportChannels.

I'd post the forum in the internals async channel. https://forum.djangoproject.com/

Essentially you're going to need to wait for async ORM support to do this properly.

Even then you'll need to consider the number of DB connections you're creating. You're trying to create a connection for each item in metrics, which is too many (This kind of issue is perennial...)

comment:2 by Carlton Gibson, 4 years ago

Just a follow-up: ORM calls should use the thread_sensitive parameter to sync_to_async() this would likely make your example run, but it would do so serially, rather than concurrently, since all the operations would be run in a single thread executor. (Maybe that helps anyway.)

Note: See TracTickets for help on using tickets.
Back to Top