Opened 6 years ago

Closed 6 years ago

#29846 closed Uncategorized (invalid)

Signals - handling multiple requests concurrently

Reported by: Anomitra Saha Owned by: nobody
Component: HTTP handling Version: 1.11
Severity: Normal Keywords: signals
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 using a pre_save signal to store the user who performed the last operation on a particular instance.

To achieve this, I'm using a middleware. In the process_request function of it, the signal is registered with the appropriate function, set_user as shown below.

mark_who_did = curry(self.set_user, user)
models.signals.pre_save.connect(mark_who_did, dispatch_uid=(self.class, request,), weak=False)

Then, I'm disconnecting this signal in the process_response function of the middleware.

models.signals.pre_save.disconnect(dispatch_uid=(self.class, request,))

This works fine when concurrency is not involved. However, suppose we have three concurrent requests - the signal is connected three times, and the set_user method is invoked nine times, for each user-request combination.

As per my understanding, each request should have been operating independently, but that is obviously not the case. Is there something I'm missing, or is there something I could change in my code to fix this?

Change History (1)

comment:1 by Simon Charette, 6 years ago

Resolution: invalid
Status: newclosed

Hello there,

If you want to achieve this using a middleware you'll most likely want to assign a thread local and use a single signal receiver to look it up but that's usually considered a bad pattern because of the global state it creates. While there's is third party apps implementing this pattern you're better off passing along request.user from your views to your forms/serializer and models instead.

Please use TicketClosingReasons/UseSupportChannels to help you figure out the missing pieces of the puzzle as this issue tracker is used to keep track of bugs and feature requests and isn't a second tier support channel.

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