관리-도구
편집 파일: sqlitelockfile.cpython-36.pyc
3 �Pf� � @ sr d dl mZmZ d dlZd dlZye W n ek r@ eZY nX ddlm Z m Z mZmZm Z G dd� de �ZdS )� )�absolute_import�divisionN� )�LockBase� NotLocked� NotMyLock�LockTimeout� AlreadyLockedc @ sP e Zd ZdZdZddd�Zddd�Zdd � Zd d� Zdd � Z dd� Z dd� ZdS )�SQLiteLockFilezDemonstrate SQL-based locking.NTc C s� t j| |||� t| j�| _t| j�| _tjdkrdddl}|j� \}}t j |� t j|� ~~|t_ddl}|j tj�| _| jj� }y|jd� W n |jk r� Y n$X | jj� ddl} | jt jtj� dS )zu >>> lock = SQLiteLockFile('somefile') >>> lock = SQLiteLockFile('somefile', threaded=False) Nr zGcreate table locks( lock_file varchar(32), unique_name varchar(32)))r �__init__�unicode� lock_file�unique_namer �testdb�tempfileZmkstemp�os�close�unlink�sqlite3Zconnect� connection�cursor�executeZOperationalError�commit�atexit�register) �self�pathZthreaded�timeoutr Z_fdr r �cr � r �$/usr/lib/python3.6/sqlitelockfile.pyr s( zSQLiteLockFile.__init__c C sH |d k r|n| j }tj� }|d k r2|dkr2||7 }|d kr@d}n|dkrNd}n|d }| jj� }x�| j� s�|jd| j| jf� | jj� |jd| jf� |j � }t |�dkr�|jd| jf� | jj� q�d S n(|jd| jf� |j � }t |�dkr�d S |d k �r6tj� |k�r6|dk�r(td| j ��nt d | j ��tj|� qbW d S ) Nr g�������?� z;insert into locks (lock_file, unique_name) values (?, ?)z*select * from locks where unique_name = ?r z(delete from locks where unique_name = ?z&Timeout waiting to acquire lock for %sz%s is already locked)r �timer r � is_lockedr r r r �fetchall�lenr r r Zsleep)r r Zend_time�waitr �rowsr r r �acquire5 sD zSQLiteLockFile.acquirec C s\ | j � std| j ��| j� s4td| j| j� f ��| jj� }|j d| jf� | jj � d S )Nz%s is not lockedz#%s is locked, but not by me (by %s)z(delete from locks where unique_name = ?)r# r r �i_am_lockingr r �_who_is_lockingr r r r )r r r r r �releaset s zSQLiteLockFile.releasec C s&