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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

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

Okay