DEV Community

Jay Z
Jay Z

Posted on

1 1

Caution with try/expect when during with concurrent requests

Look at this code:
try
example.objects.get(name=xxx)
expect Exception:
example.objects.create(name=xxx)
blah, blah

The code seems to be working, but it is not thread safe. When we have concurrent requests coming to the server. The requests would penetrate into the expect block and try to create the resource or do something, causing unexpected behavior.

Top comments (0)

Postmark Image

20% off for developers who'd rather build features than debug email

Stop wrestling with email delivery and get back to the code you love. Postmark handles the complexities of email infrastructure so you can ship your product faster.

Start free

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay