title: Getting 'Deadlock found when trying to get lock; try restarting transaction'
body: <p>I have multiprocessed application, and each thread executes the following python code:</p>
  
  <pre><code>while True:
      db_cursor.execute("SET autocommit=0;")
      db_cursor.execute("LOCK TABLES prqueue WRITE;")
      db_cursor.execute("SELECT MAX(id) FROM prqueue;")
  
      for record in db_cursor.fetchall():
          if record[0]:
              db_cursor.execute("DELETE FROM prqueue WHERE id='%s';" % record[0]);
              #db_connector.commit()
              db_cursor.execute("COMMIT;")
              db_cursor.execute("UNLOCK TABLES;")
  
              # some work that takes time
  
          else:
              db_cursor.execute("COMMIT;")
              db_cursor.execute("UNLOCK TABLES;")
  </code></pre>
  
  <p>Sometimes my processes die with OperationalError exception: <code>'Deadlock found when trying to get lock; try restarting transaction'</code>.</p>
  
  <p>And the most strange thing here is that processes die on different lines. For example, error messages from 3 of them:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "/home/crawler/Worker.py", line 122, in do_crawling
      db_cursor.execute("COMMIT;")
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
      self.errorhandler(self, exc, value)
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
      raise errorclass, errorvalue
  OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction')
  
  Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "/home/crawler/Worker.py", line 122, in do_crawling
      db_cursor.execute("COMMIT;")
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
      self.errorhandler(self, exc, value)
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
      raise errorclass, errorvalue
  OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction')
  
  Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "/home/crawler/Worker.py", line 109, in do_crawling
      db_cursor.execute("SELECT MAX(id) FROM prqueue;")
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
      self.errorhandler(self, exc, value)
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
      raise errorclass, errorvalue
  OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting transaction')
  </code></pre>
  
  <p>Am I doing something wrong in this code? Completely exhausted with all these deadlocks.</p>
  
  <p>Thank you.</p>