
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    SW[!                     @   s  d dl Z d dlZd dlmZ d dlmZ zd dlmZ W n  ek
rX   d dlmZ Y nX z0d dlZd dlm	Z
 d dlmZ d dlmZ W n  ek
r   d Z
 ZZY nX d dlmZ e dZG dd	 d	eZG d
d deZdZdZG dd deZG dd deZG dd deZdS )    NEvent)Thread)Queue)Greenlet)SqliteExtDatabasezpeewee.sqliteqc                   @   s   e Zd ZdS )ResultTimeoutN)__name__
__module____qualname__ r   r   D/opt/alt/python38/lib64/python3.8/site-packages/playhouse/sqliteq.pyr      s   r   c                   @   s|   e Zd ZdZdd ZdddZdddZd	d
 Zdd ZeZ	e
dd Ze
dd Ze
dd Zdd Zdd Zdd ZdS )AsyncCursor)	sqlparamscommittimeout_event_cursor_exc_idx_rowsc                 C   s:   || _ || _|| _|| _|| _d  | _ | _ | _| _d S N)	r   r   r   r   r   r   r   r   r   )selfeventr   r   r   r   r   r   r   __init__    s    zAsyncCursor.__init__Nc                 C   s6   || _ || _d| _|d kr"| ng | _| j  | S )Nr   )r   r   r   fetchallr   r   set)r   cursorexcr   r   r   
set_result(   s    
zAsyncCursor.set_resultc                 C   s@   |d k	r|n| j }| jj|ds,|r,td| jd k	r<| jd S )N)r   zresults not ready, timed out.)r   r   waitr   r   )r   r   r   r   r   _wait0   s
    
zAsyncCursor._waitc                 C   s   |    | jd k	r| j| S r   )r"   r   _execr   r   r   r   __iter__7   s    
zAsyncCursor.__iter__c                 C   s@   z| j | j }W n tk
r(   tY nX |  jd7  _|S d S )N   )r   r   
IndexErrorStopIteration)r   objr   r   r   next=   s    
zAsyncCursor.nextc                 C   s   |    | jjS r   )r"   r   	lastrowidr$   r   r   r   r+   G   s    zAsyncCursor.lastrowidc                 C   s   |    | jjS r   )r"   r   rowcountr$   r   r   r   r,   L   s    zAsyncCursor.rowcountc                 C   s   | j jS r   )r   descriptionr$   r   r   r   r-   Q   s    zAsyncCursor.descriptionc                 C   s   | j   d S r   )r   closer$   r   r   r   r.   U   s    zAsyncCursor.closec                 C   s   t | S r   )listr$   r   r   r   r   X   s    zAsyncCursor.fetchallc                 C   s.   |    z
t| W S  tk
r(   Y d S X d S r   )r"   r*   r(   r$   r   r   r   fetchone[   s
    
zAsyncCursor.fetchone)N)N)r	   r
   r   	__slots__r   r    r"   r%   r*   __next__propertyr+   r,   r-   r.   r   r0   r   r   r   r   r      s    




r   zthreadlocals cannot be set to True when using the Sqlite thread / queue database. All queries are serialized through a single connection, so allowing multiple threads to connect defeats the purpose of this database.zSQLite must be configured to use the WAL journal mode when using this feature. WAL mode allows one or more readers to continue reading while another connection writes to the database.c                       sr   e Zd Zd fdd	Zdd Zddd	Zd
d Zdd Zdd Zdd Z	dddZ
dd Zdd Zdd Z  ZS )SqliteQueueDatabaseFr&   Nc                    s   | drttd|d< d|d< | |dd |dd }	tt| }
|
j| _|
j	|f|d|	i| || _
|| _|| _d| _| ||| _|   | j
r|   d S )NthreadlocalsFZcheck_same_threadjournal_modepragmasT)get
ValueErrorTHREADLOCAL_ERROR_MESSAGE_validate_journal_modepopsuperr4   execute_sql!_SqliteQueueDatabase__execute_sqlr   Z
_autostart_results_timeout_num_readers_is_stoppedget_thread_impl_thread_helper_create_queues_and_workersstart)r   database
use_geventZ	autostartZreadersqueue_max_sizeZresults_timeoutargskwargsr7   ZParent	__class__r   r   r   o   s&    



zSqliteQueueDatabase.__init__c                 C   s   |rt S tS r   )GreenletHelperThreadHelper)r   rH   r   r   r   rC      s    z#SqliteQueueDatabase.get_thread_implc                 C   sh   |r|  dkrtt|r^tdd |D }|dd  dkrJttdd |D dg S dgS d S )Nwalc                 s   s   | ]\}}|  |fV  qd S r   )lower.0kvr   r   r   	<genexpr>   s     z=SqliteQueueDatabase._validate_journal_mode.<locals>.<genexpr>r6   c                 S   s    g | ]\}}|d kr||fqS )r6   r   rR   r   r   r   
<listcomp>   s    z>SqliteQueueDatabase._validate_journal_mode.<locals>.<listcomp>)r6   rP   )rQ   r9   WAL_MODE_ERROR_MESSAGEdictr8   )r   r6   r7   Zpdictr   r   r   r;      s    z*SqliteQueueDatabase._validate_journal_modec                    sP    j   _ j   _ j j  j _ fddt jD  _	d S )Nc                    s   g | ]} j  jqS r   )rD   thread_read_queue)rS   _r   targetr   r   rW      s   zBSqliteQueueDatabase._create_queues_and_workers.<locals>.<listcomp>)
rD   queue_write_queuer[   _run_worker_looprZ   _writerrangerA   _readersr$   r   r]   r   rE      s    z.SqliteQueueDatabase._create_queues_and_workersc                 C   s<   |  }|tkrtd d S td|j | | q d S )Nzworker shutting down.zreceived query %s)r8   r(   loggerinfodebugr   _process_execution)r   r_   async_cursorr   r   r   ra      s    
z$SqliteQueueDatabase._run_worker_loopc              
   C   sP   z|  |j|j|j}W n& tk
r> } zd }W 5 d }~X Y nX d }|||S r   )r?   r   r   r   	Exceptionr    )r   ri   r   r   r   r   r   rh      s    z&SqliteQueueDatabase._process_executionc                 C   s   | j  | j fS r   )r`   Zqsizer[   r$   r   r   r   
queue_size   s    zSqliteQueueDatabase.queue_sizeTc                 C   sD   t | j ||||d kr| jn|d}|r0| jn| j}|| |S )N)r   r   r   r   r   )r   rD   r   r@   r`   r[   put)r   r   r   require_commitr   r   r_   r   r   r   r>      s    
zSqliteQueueDatabase.execute_sqlc              	   C   sb   | j R | jsW 5 Q R  dS | j  | jD ]}|  q,td d| _W 5 Q R  dS Q R X d S )NFzworkers started.T)
_conn_lockrB   rb   rF   rd   re   rf   )r   readerr   r   r   rF      s    



zSqliteQueueDatabase.startc              	   C   s   t d | jf | jr&W 5 Q R  dS | jt | jD ]}| jt q8| j	
  | jD ]}|
  qZW 5 Q R  dS Q R X d S )Nzenvironment stop requested.FT)re   rg   rn   rB   r`   rl   r(   rd   r[   rb   join)r   r\   ro   r   r   r   stop   s    




zSqliteQueueDatabase.stopc              
   C   s$   | j  | jW  5 Q R  S Q R X d S r   )rn   rB   r$   r   r   r   
is_stopped   s    zSqliteQueueDatabase.is_stopped)FFr&   NN)NN)NTN)r	   r
   r   r   rC   r;   rE   ra   rh   rk   r>   rF   rq   rr   __classcell__r   r   rL   r   r4   n   s       !
	


r4   c                   @   s4   e Zd ZdZdddZdd ZdddZd	d
 ZdS )rO   rI   Nc                 C   s
   || _ d S r   rt   )r   rI   r   r   r   r      s    zThreadHelper.__init__c                 C   s   t  S r   r   r$   r   r   r   r          zThreadHelper.eventc                 C   s    |d k	r|n| j }t|pddS Nr   )maxsize)rI   r   r   Zmax_sizer   r   r   r_      s    zThreadHelper.queuec                 O   s   t |||d}d|_|S )N)r^   rJ   rK   T)r   daemon)r   fnrJ   rK   rZ   r   r   r   rZ      s    zThreadHelper.thread)N)N)r	   r
   r   r1   r   r   r_   rZ   r   r   r   r   rO      s
   

rO   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
rN   rt   c                 C   s   t  S r   )GEventr$   r   r   r   r      ru   zGreenletHelper.eventNc                 C   s    |d k	r|n| j }t|pddS rv   )rI   GQueuerx   r   r   r   r_      s    zGreenletHelper.queuec                    s    fdd}t |f||S )Nc                     s   t    | |S r   )geventsleep)arT   rz   r   r   wrap  s    z#GreenletHelper.thread.<locals>.wrap)GThread)r   rz   rJ   rK   r   r   r   r   rZ     s    zGreenletHelper.thread)N)r	   r
   r   r1   r   r_   rZ   r   r   r   r   rN      s   
rN   )loggingweakref	threadingr   r   r   ImportErrorr_   r}   r   r   Zgevent.eventr{   Zgevent.queuer|   Zplayhouse.sqlite_extr   	getLoggerre   rj   r   objectr   r:   rX   r4   rO   rN   r   r   r   r   <module>   s.   
G{