(self, timeout=None)
| 51 | atexit.register(os.unlink, SQLiteLockFile.testdb) |
| 52 | |
| 53 | def acquire(self, timeout=None): |
| 54 | timeout = timeout if timeout is not None else self.timeout |
| 55 | end_time = time.time() |
| 56 | if timeout is not None and timeout > 0: |
| 57 | end_time += timeout |
| 58 | |
| 59 | if timeout is None: |
| 60 | wait = 0.1 |
| 61 | elif timeout <= 0: |
| 62 | wait = 0 |
| 63 | else: |
| 64 | wait = timeout / 10 |
| 65 | |
| 66 | cursor = self.connection.cursor() |
| 67 | |
| 68 | while True: |
| 69 | if not self.is_locked(): |
| 70 | # Not locked. Try to lock it. |
| 71 | cursor.execute("insert into locks" |
| 72 | " (lock_file, unique_name)" |
| 73 | " values" |
| 74 | " (?, ?)", |
| 75 | (self.lock_file, self.unique_name)) |
| 76 | self.connection.commit() |
| 77 | |
| 78 | # Check to see if we are the only lock holder. |
| 79 | cursor.execute("select * from locks" |
| 80 | " where unique_name = ?", |
| 81 | (self.unique_name,)) |
| 82 | rows = cursor.fetchall() |
| 83 | if len(rows) > 1: |
| 84 | # Nope. Someone else got there. Remove our lock. |
| 85 | cursor.execute("delete from locks" |
| 86 | " where unique_name = ?", |
| 87 | (self.unique_name,)) |
| 88 | self.connection.commit() |
| 89 | else: |
| 90 | # Yup. We're done, so go home. |
| 91 | return |
| 92 | else: |
| 93 | # Check to see if we are the only lock holder. |
| 94 | cursor.execute("select * from locks" |
| 95 | " where unique_name = ?", |
| 96 | (self.unique_name,)) |
| 97 | rows = cursor.fetchall() |
| 98 | if len(rows) == 1: |
| 99 | # We're the locker, so go home. |
| 100 | return |
| 101 | |
| 102 | # Maybe we should wait a bit longer. |
| 103 | if timeout is not None and time.time() > end_time: |
| 104 | if timeout > 0: |
| 105 | # No more waiting. |
| 106 | raise LockTimeout("Timeout waiting to acquire" |
| 107 | " lock for %s" % |
| 108 | self.path) |
| 109 | else: |
| 110 | # Someone else has the lock and we are impatient.. |
nothing calls this directly
no test coverage detected