Python threading.RLock() Examples

The following are 30 code examples for showing how to use threading.RLock(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module threading , or try the search function .

Example 1
Project: jawfish   Author: war-and-code   File: __init__.py    License: MIT License 6 votes vote down vote up
def _checkLevel(level):
    if isinstance(level, int):
        rv = level
    elif str(level) == level:
        if level not in _levelNames:
            raise ValueError("Unknown level: %r" % level)
        rv = _levelNames[level]
    else:
        raise TypeError("Level not an integer or a valid string: %r" % level)
    return rv

#---------------------------------------------------------------------------
#   Thread-related stuff
#---------------------------------------------------------------------------

#
#_lock is used to serialize access to shared data structures in this module.
#This needs to be an RLock because fileConfig() creates and configures
#Handlers, and so might arbitrary user threads. Since Handler code updates the
#shared dictionary _handlers, it needs to acquire the lock. But if configuring,
#the lock would already have been acquired - so we need an RLock.
#The same argument applies to Loggers and Manager.loggerDict.
# 
Example 2
Project: tornado-zh   Author: tao12345666333   File: template.py    License: MIT License 6 votes vote down vote up
def __init__(self, autoescape=_DEFAULT_AUTOESCAPE, namespace=None,
                 whitespace=None):
        """构造一个模板加载器.

        :arg str autoescape: 在模板命名空间中的函数名, 例如 "xhtml_escape",
            或默认情况下为 ``None`` 来禁用自动转义.
        :arg dict namespace: 一个被加入默认模板命名空间中的字典或 ``None``.
        :arg str whitespace: 一个指定模板中whitespace默认行为的字符串;
            参见 `filter_whitespace` 查看可选项. 默认是 "single" 对于
            ".html" 和 ".js" 文件的结束, "all" 是为了其他文件.

        .. versionchanged:: 4.3
           添加 ``whitespace`` 参数.
        """
        self.autoescape = autoescape
        self.namespace = namespace or {}
        self.whitespace = whitespace
        self.templates = {}
        # self.lock protects self.templates.  It's a reentrant lock
        # because templates may load other templates via `include` or
        # `extends`.  Note that thanks to the GIL this code would be safe
        # even without the lock, but could lead to wasted work as multiple
        # threads tried to compile the same template simultaneously.
        self.lock = threading.RLock() 
Example 3
Project: tornado-zh   Author: tao12345666333   File: template.py    License: MIT License 6 votes vote down vote up
def __init__(self, autoescape=_DEFAULT_AUTOESCAPE, namespace=None,
                 whitespace=None):
        """构造一个模板加载器.

        :arg str autoescape: 在模板命名空间中的函数名, 例如 "xhtml_escape",
            或默认情况下为 ``None`` 来禁用自动转义.
        :arg dict namespace: 一个被加入默认模板命名空间中的字典或 ``None``.
        :arg str whitespace: 一个指定模板中whitespace默认行为的字符串;
            参见 `filter_whitespace` 查看可选项. 默认是 "single" 对于
            ".html" 和 ".js" 文件的结束, "all" 是为了其他文件.

        .. versionchanged:: 4.3
           添加 ``whitespace`` 参数.
        """
        self.autoescape = autoescape
        self.namespace = namespace or {}
        self.whitespace = whitespace
        self.templates = {}
        # self.lock protects self.templates.  It's a reentrant lock
        # because templates may load other templates via `include` or
        # `extends`.  Note that thanks to the GIL this code would be safe
        # even without the lock, but could lead to wasted work as multiple
        # threads tried to compile the same template simultaneously.
        self.lock = threading.RLock() 
Example 4
Project: ratelimit   Author: tomasbasham   File: decorators.py    License: MIT License 6 votes vote down vote up
def __init__(self, calls=15, period=900, clock=now(), raise_on_limit=True):
        '''
        Instantiate a RateLimitDecorator with some sensible defaults. By
        default the Twitter rate limiting window is respected (15 calls every
        15 minutes).

        :param int calls: Maximum function invocations allowed within a time period.
        :param float period: An upper bound time period (in seconds) before the rate limit resets.
        :param function clock: An optional function retuning the current time.
        :param bool raise_on_limit: A boolean allowing the caller to avoiding rasing an exception.
        '''
        self.clamped_calls = max(1, min(sys.maxsize, floor(calls)))
        self.period = period
        self.clock = clock
        self.raise_on_limit = raise_on_limit

        # Initialise the decorator state.
        self.last_reset = clock()
        self.num_calls = 0

        # Add thread safety.
        self.lock = threading.RLock() 
Example 5
Project: arctic   Author: man-group   File: incremental.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, serializer, input_data, chunk_size, string_max_len=None):
        super(IncrementalPandasToRecArraySerializer, self).__init__(serializer, input_data, chunk_size)
        if not isinstance(serializer, PandasSerializer):
            raise ArcticSerializationException("IncrementalPandasToRecArraySerializer requires a serializer of "
                                               "type PandasSerializer.")
        if not isinstance(input_data, (pd.DataFrame, pd.Series)):
            raise ArcticSerializationException("IncrementalPandasToRecArraySerializer requires a pandas DataFrame or "
                                               "Series as data source input.")
        if string_max_len and string_max_len < 1:
            raise ArcticSerializationException("IncrementalPandasToRecArraySerializer can't be initialized "
                                               "with string_max_len < 1 ({})".format(string_max_len))
        self.string_max_len = string_max_len
        # The state which needs to be lazily initialized
        self._dtype = None
        self._shape = None
        self._rows_per_chunk = 0
        self._total_chunks = 0
        self._has_string_object = False
        self._lock = RLock() 
Example 6
Project: arctic   Author: man-group   File: _workers_pool.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, pool_size):
        # Only allow creation via get_instance
        if not type(self)._SINGLETON_LOCK._is_owned():
            raise AsyncArcticException("{} is a singleton, can't create a new instance".format(type(self)))

        pool_size = int(pool_size)
        if pool_size < 1:
            raise ValueError("{} can't be instantiated with a pool_size of {}".format(type(self), pool_size))

        # Enforce the singleton pattern
        with type(self)._SINGLETON_LOCK:
            if type(self)._instance is not None:
                raise AsyncArcticException("LazySingletonTasksCoordinator is a singleton, can't create a new instance")
            self._lock = RLock()
            self._pool = None
            self._pool_size = int(pool_size)
            self._pool_update_hooks = []
            self.alive_tasks = {}
            self.is_shutdown = False 
Example 7
Project: recruit   Author: Frank-qlu   File: locators.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, url, timeout=None, num_workers=10, **kwargs):
        """
        Initialise an instance.
        :param url: The root URL to use for scraping.
        :param timeout: The timeout, in seconds, to be applied to requests.
                        This defaults to ``None`` (no timeout specified).
        :param num_workers: The number of worker threads you want to do I/O,
                            This defaults to 10.
        :param kwargs: Passed to the superclass.
        """
        super(SimpleScrapingLocator, self).__init__(**kwargs)
        self.base_url = ensure_slash(url)
        self.timeout = timeout
        self._page_cache = {}
        self._seen = set()
        self._to_fetch = queue.Queue()
        self._bad_hosts = set()
        self.skip_externals = False
        self.num_workers = num_workers
        self._lock = threading.RLock()
        # See issue #45: we need to be resilient when the locator is used
        # in a thread, e.g. with concurrent.futures. We can't use self._lock
        # as it is for coordinating our internal threads - the ones created
        # in _prepare_threads.
        self._gplock = threading.RLock() 
Example 8
Project: browserscope   Author: elsigh   File: application_configuration.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, app_yaml_path, backend_yaml_path):
    """Initializer for BackendsConfiguration.

    Args:
      app_yaml_path: A string containing the full path of the yaml file
          containing the configuration for this server.
      backend_yaml_path: A string containing the full path of the backends.yaml
          file containing the configuration for backends.
    """
    self._update_lock = threading.RLock()
    self._base_server_configuration = ServerConfiguration(app_yaml_path)
    backend_info_external = self._parse_configuration(
        backend_yaml_path)

    self._backends_name_to_backend_entry = {}
    for backend in backend_info_external.backends or []:
      self._backends_name_to_backend_entry[backend.name] = backend
    self._changes = dict(
        (backend_name, set())
        for backend_name in self._backends_name_to_backend_entry) 
Example 9
Project: cherrypy   Author: cherrypy   File: sessions.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def acquire_lock(self):
        """Acquire an exclusive lock on the currently-loaded session data."""
        self.locked = True
        self.locks.setdefault(self.id, threading.RLock()).acquire() 
Example 10
Project: cherrypy   Author: cherrypy   File: sessions.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def acquire_lock(self):
        """Acquire an exclusive lock on the currently-loaded session data."""
        self.locked = True
        self.locks.setdefault(self.id, threading.RLock()).acquire()
        if self.debug:
            cherrypy.log('Lock acquired.', 'TOOLS.SESSIONS') 
Example 11
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: rlocks.py    License: MIT License 5 votes vote down vote up
def __init__(self):
    self.a = 1
    self.b = 2
    self.rlock = threading.RLock() 
Example 12
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: rlocks.py    License: MIT License 5 votes vote down vote up
def modifyA(self):
    with self.rlock:
      print("Modifying A : RLock Acquired: {}".format(self.rlock._is_owned()))
      print("{}".format(self.rlock))
      self.a = self.a + 1
      time.sleep(5) 
Example 13
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: rlocks.py    License: MIT License 5 votes vote down vote up
def modifyB(self):
    with self.rlock:
      print("Modifying B : RLock Acquired: {}".format(self.rlock._is_owned()))
      print("{}".format(self.rlock))
      self.b = self.b - 1
      time.sleep(5) 
Example 14
Project: iSDX   Author: sdn-ixp   File: peer.py    License: Apache License 2.0 5 votes vote down vote up
def getlock(self, prefix):
        if prefix not in self.prefix_lock:
            self.prefix_lock[prefix] = RLock()
        return self.prefix_lock[prefix] 
Example 15
Project: iSDX   Author: sdn-ixp   File: participant_controller.py    License: Apache License 2.0 5 votes vote down vote up
def getlock(self, prefixes):
        prefixes.sort()
        hsh = "-".join(prefixes)
        if hsh not in self.prefix_lock:
            #self.logger.debug("First Lock:: "+str(hsh))
            self.prefix_lock[hsh] = RLock()
        #else:
            #self.logger.debug("Repeat :: "+str(hsh))
        return self.prefix_lock[hsh] 
Example 16
Project: clashroyale   Author: cgrok   File: utils.py    License: MIT License 5 votes vote down vote up
def __init__(self, filename, table_name='data', fast_save=False, **options):
        self.filename = filename
        self.table_name = table_name
        self.fast_save = fast_save
        self.can_commit = True
        self._bulk_commit = False
        self._pending_connection = None
        self._lock = threading.RLock()
        with self.connection() as con:
            con.execute("create table if not exists `%s` (key PRIMARY KEY, value)" % self.table_name) 
Example 17
Project: clashroyale   Author: cgrok   File: utils.py    License: MIT License 5 votes vote down vote up
def __init__(self, filename, table_name='data', fast_save=False, **options):
        self.filename = filename
        self.table_name = table_name
        self.fast_save = fast_save
        self.can_commit = True
        self._bulk_commit = False
        self._pending_connection = None
        self._lock = threading.RLock()
        with self.connection() as con:
            con.execute("create table if not exists `%s` (key PRIMARY KEY, value)" % self.table_name) 
Example 18
Project: leveldb-py   Author: jtolio   File: leveldb.py    License: MIT License 5 votes vote down vote up
def __init__(self, data=None, is_snapshot=False):
        if data is None:
            self._data = []
        else:
            self._data = data
        self._lock = threading.RLock()
        self._is_snapshot = is_snapshot 
Example 19
Project: jawfish   Author: war-and-code   File: _threading_local.py    License: MIT License 5 votes vote down vote up
def __new__(cls, *args, **kw):
        if (args or kw) and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")
        self = object.__new__(cls)
        impl = _localimpl()
        impl.localargs = (args, kw)
        impl.locallock = RLock()
        object.__setattr__(self, '_local__impl', impl)
        # We need to create the thread dict in anticipation of
        # __init__ being called, to make sure we don't call it
        # again ourselves.
        impl.create_dict()
        return self 
Example 20
Project: jawfish   Author: war-and-code   File: __init__.py    License: MIT License 5 votes vote down vote up
def createLock(self):
        """
        Acquire a thread lock for serializing access to the underlying I/O.
        """
        if threading:
            self.lock = threading.RLock()
        else: #pragma: no cover
            self.lock = None 
Example 21
Project: jawfish   Author: war-and-code   File: _collections.py    License: MIT License 5 votes vote down vote up
def __init__(self, maxsize=10, dispose_func=None):
        self._maxsize = maxsize
        self.dispose_func = dispose_func

        self._container = self.ContainerCls()
        self.lock = RLock() 
Example 22
Project: jawfish   Author: war-and-code   File: cookies.py    License: MIT License 5 votes vote down vote up
def __getstate__(self):
        """Unlike a normal CookieJar, this class is pickleable."""
        state = self.__dict__.copy()
        # remove the unpickleable RLock object
        state.pop('_cookies_lock')
        return state 
Example 23
Project: gist-alfred   Author: danielecook   File: _collections.py    License: MIT License 5 votes vote down vote up
def __init__(self, maxsize=10, dispose_func=None):
        self._maxsize = maxsize
        self.dispose_func = dispose_func

        self._container = self.ContainerCls()
        self.lock = RLock() 
Example 24
Project: gist-alfred   Author: danielecook   File: cookies.py    License: MIT License 5 votes vote down vote up
def __getstate__(self):
        """Unlike a normal CookieJar, this class is pickleable."""
        state = self.__dict__.copy()
        # remove the unpickleable RLock object
        state.pop('_cookies_lock')
        return state 
Example 25
Project: gist-alfred   Author: danielecook   File: cookies.py    License: MIT License 5 votes vote down vote up
def __setstate__(self, state):
        """Unlike a normal CookieJar, this class is pickleable."""
        self.__dict__.update(state)
        if '_cookies_lock' not in self.__dict__:
            self._cookies_lock = threading.RLock() 
Example 26
Project: verge3d-blender-addon   Author: Soft8Soft   File: cookiejar.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, policy=None):
        if policy is None:
            policy = DefaultCookiePolicy()
        self._policy = policy

        self._cookies_lock = _threading.RLock()
        self._cookies = {} 
Example 27
Project: uplink   Author: prkumar   File: ratelimit.py    License: MIT License 5 votes vote down vote up
def __init__(self, max_calls, period, clock):
        self._max_calls = max_calls
        self._period = period
        self._clock = clock
        self._lock = threading.RLock()
        self._reset() 
Example 28
Project: python-podman   Author: containers   File: tunnel.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, sweap=25):
        """Construct portal, reap tunnels every sweap seconds."""
        self.data = collections.OrderedDict()
        self.sweap = sweap
        self.ttl = sweap * 2
        self.lock = threading.RLock()
        self._schedule_reaper() 
Example 29
Project: pyspider   Author: binux   File: rabbitmq.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, name, amqp_url='amqp://guest:guest@localhost:5672/%2F',
                 maxsize=0, lazy_limit=True):
        """
        Constructor for a PikaQueue.

        Not works with python 3. Default for python 2.

        amqp_url:   https://www.rabbitmq.com/uri-spec.html
        maxsize:    an integer that sets the upperbound limit on the number of
                    items that can be placed in the queue.
        lazy_limit: as rabbitmq is shared between multipul instance, for a strict
                    limit on the number of items in the queue. PikaQueue have to
                    update current queue size before every put operation. When
                    `lazy_limit` is enabled, PikaQueue will check queue size every
                    max_size / 10 put operation for better performace.
        """
        self.name = name
        self.amqp_url = amqp_url
        self.maxsize = maxsize
        self.lock = threading.RLock()

        self.lazy_limit = lazy_limit
        if self.lazy_limit and self.maxsize:
            self.qsize_diff_limit = int(self.maxsize * 0.1)
        else:
            self.qsize_diff_limit = 0
        self.qsize_diff = 0

        self.reconnect() 
Example 30
Project: pyspider   Author: binux   File: rabbitmq.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, name, amqp_url='amqp://guest:guest@localhost:5672/%2F',
                 maxsize=0, lazy_limit=True):
        """
        Constructor for a AmqpQueue.

        Default for python 3.

        amqp_url:   https://www.rabbitmq.com/uri-spec.html
        maxsize:    an integer that sets the upperbound limit on the number of
                    items that can be placed in the queue.
        lazy_limit: as rabbitmq is shared between multipul instance, for a strict
                    limit on the number of items in the queue. PikaQueue have to
                    update current queue size before every put operation. When
                    `lazy_limit` is enabled, PikaQueue will check queue size every
                    max_size / 10 put operation for better performace.
        """
        self.name = name
        self.amqp_url = amqp_url
        self.maxsize = maxsize
        self.lock = threading.RLock()

        self.lazy_limit = lazy_limit
        if self.lazy_limit and self.maxsize:
            self.qsize_diff_limit = int(self.maxsize * 0.1)
        else:
            self.qsize_diff_limit = 0
        self.qsize_diff = 0

        self.reconnect()