title: How can I execute some part of code once at a time in a concurrently running function?
body: <p>So, let me give you a brief of what am trying to achieve. I've a command called <code>apply</code>, as soon as some one enters that command, execution of apply function begins. Now, apply function have some part of code which I want to be executed concurrently in case if many users triggered that command at the same time, and there is a async function called <code>register</code> which I want to be executed once at a time. Here's the glimpse of the code:</p>
  <pre><code>@client.command()
  async def apply(ctx):
   #Stuff I want to be run concurrently
   await register(ctx.author)
  
  async def register(user):
   # I've an another python file 'db.py' for accessing the database 
   data = await db.execute_query('SELECT Total_Reg from Data WHERE rowid = 1')
   total = data[0][0] # Getting the total registrations value and storing it in total variable     
   if total &gt;= max_reg: # If total reg. are more or equal to max value then close the reg.
    print(&quot;Registrations are closed!&quot;)
    return
   # Otherwise if registrations are open then increase the value of total reg by 1
   # And register the user with unique registration number [reg_no] allotted to him
   await db.execute_query('UPDATE Data SET Total_Reg = Total_Reg + 1 WHERE rowid = 1')
   reg_no = total + 1
   registrations[reg_no] = user
   await close_registration() 
   &quot;&quot;&quot;Also, in case of Queue or execution of this function once at a time, I 
      want this function to continue executing the code and shouldn't wait for 
      this line of code particularly, i.e await close_registration() should 
      keep on executing in background without blocking the current execution of 
      this function. So should I use asyncio.ensure_future(close_registration()) 
      instead of await or what?&quot;&quot;&quot;
  </code></pre>
  <p>I hope you get the idea what am trying to do here. Now, let's come to my query.
  <code>apply</code> function is triggered by discord as soon as some one enters <code>apply</code> command, so there can be some case that <code>data = await db.execute_query('SELECT Total_Reg from Data WHERE rowid = 1')</code> can be executed multiple times at the same time which will cause the same number of registrations at that time to be returned and stored in total variable of all of them.</p>
  <p>I know, sqlite locks the database when there is already a connection updating or accessing the database but then also, as soon as a query is executed, another will start executing there itself before <code>await db.execute_query('UPDATE Data SET Total_Reg = Total_Reg + 1 WHERE rowid = 1')</code> Increment query is executed.</p>
  <p>What I want basically is,</p>
  <p>This should be the order:</p>
  <ol>
  <li>Getting the current number of registrations with <code> data = await db.execute_query('SELECT Total_Reg from Data WHERE rowid = 1')</code></li>
  <li>Checking if registration is full or not</li>
  <li>If not, then register the user and update the database by increasing the current no. of registrations by 1 with <code>await db.execute_query('UPDATE Data SET Total_Reg = Total_Reg + 1 WHERE rowid = 1')</code></li>
  </ol>
  <p>For example, if 2 users applied to register at the same time, then one should be registered first and then only another should get registered and <code>reg_no</code> for both of them should be unique.</p>
  <p>So, that's it. How can I achieve this functionality? Should I use a Queue? Or is there any feature in sqlite to first get the current reg. and then increase the value by 1 if it's less than <code>max_reg</code> and all this in just a single query?</p>
  <p>Thanks!</p>