title: Multiprocess sqlite INSERT: "database is locked"
body: <p><em>(Please note: There is a question called "<strong>SQLite3 and Multiprocessing</strong>" but that question is actually about <strong>multithreading</strong> and so is the accepted answer, this isn't a duplicate)</em></p>
  
  <p>I'm implementing a multiprocess script, each process will need to write some results in an sqlite table. My program keeps crashing with <code>database is locked</code> (with sqlite only one DB modification is allowed at a time).</p>
  
  <p>Here's an example of what I have:</p>
  
  <pre><code>def scan(n):
      n = n + 1 # Some calculation
  
      cur.execute("                      \
                      INSERT INTO hello  \
                      (n)                \
                      VALUES ('"+n+"')   \
                  ")
  
      con.commit()
      con.close()
  
      return True
  
  
  if __name__ == '__main__':
  
      pool = Pool(processes=int(sys.argv[1]))
  
      for status in pool.imap_unordered(scan, range(0,9999)):
          if status:
              print "ok"
  
      pool.close()
  </code></pre>
  
  <p>I've tried using a lock by declaring a lock in the main and using it as a global in <code>scan()</code>, but it didn't stop me getting the <code>database is locked</code>.</p>
  
  <p><strong>What is the proper way of making sure only one INSERT statement will get issued at the same time in a multiprocess Python script?</strong></p>
  
  <p>EDIT:</p>
  
  <p>I'm running on a Debian-based Linux.</p>