0

We have a server which is written in Sanic async/Python.

I want to add a app.ctx variable, but this variable would be modified by an API called parse. This parse API supports concurrent calls.

Perhaps every parse API call will add value/remove value to this variable, and these API would share some information by this variable.

I want to make sure this variable is fiber/aysnc function safe. How does Sanic support this thinking?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • What do you mean by "concurrency"? in python that can mean three very different things. Use of multiprocessing, threading, and asyncio. – Adam Hopkins Aug 08 '22 at 14:52
  • hi Adam. yes, I think Sanic is asyncio style. I am facing a question, that is in asyncio, I plan to store a shared variable, this variable shared by different Coroutine and maintain it. is it a make sense question if we require in asyncio environment, we could keep variable safe, for example , – wang harvey Aug 09 '22 at 03:07
  • Ahh, yes. The variables are certainly available within a single instance. If you have multiple workers or instances, you will need another method to sync them. Take a look at asyncio.Lock. You can use that to protect against race conditions. – Adam Hopkins Aug 09 '22 at 18:41

0 Answers0