
    !}g                         d Z dZdZdZdZddlZddlZddlZddlm	Z	 dd	l
mZ  ej        e          Z G d
 de          Z G d de	          Z G d d          Z G d de          ZdS )z
Anything that has to do with threading in this library
must be abstracted in this file. If we decide to do gevent
also, it will deserve its own gevent file.
	newspaperzLucas Ou-YangMITzCopyright 2014, Lucas Ou-Yang    N)Thread   )Configurationc                       e Zd ZdS )ConcurrencyExceptionN)__name__
__module____qualname__     T/var/www/py-google-trends/myenv/lib/python3.11/site-packages/newspaper/mthreading.pyr	   r	      s        Dr   r	   c                       e Zd ZdZd Zd ZdS )Workerz:
    Thread executing tasks from a given tasks queue.
    c                     t          j        |            || _        || _        d| _        |                                  d S )NT)r   __init__taskstimeoutdaemonstart)selfr   timeout_secondss      r   r   zWorker.__init__   s8    
&

r   c                 
   	 	 | j                             | j                  \  }}}n# t          j        $ r Y d S w xY w	  ||i | n## t
          $ r t          j                     Y nw xY w| j                                          )NT)r   )	r   getr   queueEmpty	Exception	traceback	print_exc	task_doner   funcargskargss       r   runz
Worker.run&   s    	#$(JNN4<N$H$H!dEE;   &d$e$$$$ & & &#%%%%%& J  """	#s   $( ;;A A('A(N)r
   r   r   __doc__r   r&   r   r   r   r   r      s<           # # # # #r   r   c                        e Zd Zd Zd Zd ZdS )
ThreadPoolc                     t          j        |          | _        t          |          D ]}t	          | j        |           d S N)r   Queuer   ranger   )r   num_threadsr   _s       r   r   zThreadPool.__init__6   sI    [--
{## 	0 	0A4:////	0 	0r   c                 @    | j                             |||f           d S r+   )r   putr"   s       r   add_taskzThreadPool.add_task;   s#    
dE*+++++r   c                 8    | j                                          d S r+   )r   joinr   s    r   wait_completionzThreadPool.wait_completion>   s    
r   N)r
   r   r   r   r2   r6   r   r   r   r)   r)   5   sA        0 0 0
, , ,    r   r)   c                   $    e Zd ZddZd ZddZdS )NewsPoolNc                 >    d| _         |pt                      | _        dS )aJ  
        Abstraction of a threadpool. A newspool can accept any number of
        source OR article objects together in a list. It allocates one
        thread to every source and then joins.

        We allocate one thread per source to avoid rate limiting.
        5 sources = 5 threads, one per source.

        >>> import newspaper
        >>> from newspaper import news_pool

        >>> cnn_paper = newspaper.build('http://cnn.com')
        >>> tc_paper = newspaper.build('http://techcrunch.com')
        >>> espn_paper = newspaper.build('http://espn.com')

        >>> papers = [cnn_paper, tc_paper, espn_paper]
        >>> news_pool.set(papers)
        >>> news_pool.join()

        # All of your papers should have their articles html all populated now.
        >>> cnn_paper.articles[50].html
        u'<html>blahblah ... '
        N)poolr   config)r   r;   s     r   r   zNewsPool.__init__D   s    0 	/r   c                 r    | j         t          d          | j                                          d| _         dS )zf
        Runs the mtheading and returns when all threads have joined
        resets the task.
        NzCCall set(..) with a list of source objects before calling .join(..))r:   r	   r6   r5   s    r   r4   zNewsPool.join_   sF    
 9& (B C C C	!!###			r   r   c                 h   ddl m ||}n0t          fd|D                       r|t          |          z  }nd}| j        j        }t          ||          | _        |D ]Q}t          |          r | j        	                    |j
                   2| j        	                    |j                   RdS )a  
        news_list can be a list of `Article`, `Source`, or both.

        If caller wants to decide how many threads to use, they can use
        `override_threads` which takes precedence over all. Otherwise,
        this api infers that if the input is all `Source` objects, to
        allocate one thread per `Source` to not spam the host.

        If both of the above conditions are not true, default to 1 thread.
        r   )SourceNc                 0    g | ]}t          |          S r   )
isinstance).0nr>   s     r   
<listcomp>z NewsPool.set.<locals>.<listcomp>y   s#    ;;;A*Q'';;;r   )sourcer>   alllenr;   thread_timeout_secondsr)   r:   r@   r2   download_articlesdownload)r   	news_listthreads_per_sourceoverride_threadsr.   r   news_objectr>   s          @r   setzNewsPool.setj   s     	#"""""'*KK;;;;;;;<< 	,s9~~=KKK+4{G44	$ 	9 	9K+v.. 9	"";#@AAAA	"";#78888		9 	9r   r+   )r   N)r
   r   r   r   r4   rN   r   r   r   r8   r8   B   sK        0 0 0 06	 	 	9 9 9 9 9 9r   r8   )r'   	__title__
__author____license____copyright__loggingr   r   	threadingr   configurationr   	getLoggerr
   logr   r	   r   r)   objectr8   r   r   r   <module>rY      s3   
 	
/             ( ( ( ( ( (g!!	 	 	 	 	9 	 	 	# # # # #V # # #4
 
 
 
 
 
 
 
C9 C9 C9 C9 C9v C9 C9 C9 C9 C9r   