
    2Bfx                         d dl Z d dlZd dlZ	 d dlZddlmZ  e j                  d      Z G d de      Z	y# e$ r dZY -w xY w)    N   )PubSubManagersocketioc                   N     e Zd ZdZdZ	 	 d	 fd	Z fdZd Zd Zd Z	d Z
 xZS )
RedisManageraC  Redis based client manager.

    This class implements a Redis backend for event sharing across multiple
    processes. Only kept here as one more example of how to build a custom
    backend, since the kombu backend is perfectly adequate to support a Redis
    message queue.

    To use a Redis backend, initialize the :class:`Server` instance as
    follows::

        url = 'redis://hostname:port/0'
        server = socketio.Server(client_manager=socketio.RedisManager(url))

    :param url: The connection URL for the Redis server. For a default Redis
                store running on the same host, use ``redis://``.
    :param channel: The channel name on which the server sends and receives
                    notifications. Must be the same in all the servers.
    :param write_only: If set to ``True``, only initialize to emit events. The
                       default of ``False`` initializes the class for emitting
                       and receiving.
    :param redis_options: additional keyword arguments to be passed to
                          ``Redis.from_url()``.
    redisc                     t         t        d      || _        |xs i | _        | j	                          t
        t        |   |||       y )NzLRedis package is not installed (Run "pip install redis" in your virtualenv).)channel
write_onlylogger)r   RuntimeError	redis_urlredis_options_redis_connectsuperr   __init__)selfurlr
   r   r   r   	__class__s         V/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/socketio/redis_manager.pyr   zRedisManager.__init__)   sX    =  . / / *0blD*76@28 	+ 	:    c                    t         t        |           d}| j                  j                  dk(  rddlm}  |d      }n&d| j                  j                  v rddlm}  |d      }|s"t        d| j                  j                  z         y )	NTeventletr   )is_monkey_patchedsocketgevent)is_module_patchedz<Redis requires a monkey patched socket library to work with )
r   r   
initializeserver
async_modeeventlet.patcherr   gevent.monkeyr   r   )r   monkey_patchedr   r   r   s       r   r   zRedisManager.initialize6   s    lD,.;;!!Z/:.x8N///7.x8N++0012 2 r   c                     t        j                  j                  | j                  fi | j                  | _         | j                   j                         | _        y )N)r   Redisfrom_urlr   r   pubsub)r   s    r   r   zRedisManager._redis_connectE   sB    [[))$.. @,0,>,>@
jj'')r   c                 J   d}	 	 |s| j                          | j                  j                  | j                  t	        j
                  |            S # t        j                  j                  $ r4 |rt        j                  d       d}nt        j                  d       Y y Y nw xY w)NTz#Cannot publish to redis... retryingFz$Cannot publish to redis... giving up)
r   r   publishr
   pickledumps
exceptions
RedisErrorr   error)r   dataretrys      r   _publishzRedisManager._publishJ   s    
'')zz))$,,T8JKK##.. LL!FG!ELL!GH  s   A
A AB! B!c              #     K   d}d}	 	 |r7| j                          | j                  j                  | j                         d}| j                  j	                         D ]  }|  	 _# t
        j                  j                  $ rJ t        j                  dj                  |             d}t        j                  |       |dz  }|dkD  rd}Y gw xY ww)Nr   FTz0Cannot receive from redis... retrying in {} secs   <   )r   r'   	subscriber
   listenr   r,   r-   r   r.   formattimesleep)r   retry_sleepconnectmessages       r   _redis_listen_with_retriesz'RedisManager._redis_listen_with_retriesY   s     %'')KK))$,,7"#K#{{113 "G!M"  ##.. % 3396+3FH

;'q #"$K%s*   CAA' %C'A$CCCCc              #   <  K   | j                   j                  d      }| j                  j                  | j                          | j	                         D ]   }|d   |k(  s|d   dk(  sd|v s|d    " | j                  j                  | j                          y w)Nzutf-8r
   typer<   r/   )r
   encoder'   r5   r=   unsubscribe)r   r
   r<   s      r   _listenzRedisManager._listenm   s     ,,%%g.dll+668 	&Gy!W,FOy0Vw5Ffo%	& 	-s   ABB(B-/B)zredis://localhost:6379/0r   FNN)__name__
__module____qualname____doc__namer   r   r   r1   r=   rB   __classcell__)r   s   @r   r   r      s3    . D?I>B:2*
%(.r   r   )
loggingr*   r8   r   ImportErrorpubsub_managerr   	getLoggerr   r    r   r   <module>rN      sQ       *			:	&e.= e.  Es   6 A A 