Python threading.Lock() Examples

The following are 30 code examples for showing how to use threading.Lock(). 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 want to check out the right sidebar which shows the related API usage.

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

Example 1
Project: BASS   Author: Cisco-Talos   File: core.py    License: GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, maxsize = 0, worker_threads = 1, unpack_threads = 1, inspect_threads = 1, idb_threads = 1, bindiff_threads = 1):
        """
            Create a Bass server.
            :param maxsize: Maximum size of the job queue. If the queue is full, jobs are rejected. 0 means unlimited.
            :param threads: Number of worker threads to use.
        """

        #TODO: Access to jobs is not threadsafe
        self.job_counter = 1
        self.jobs = {}
        self.jobs_lock = Lock()
        self.input_queue = Queue(maxsize)
        self.unpack_executor = ThreadPoolExecutor(max_workers = unpack_threads)
        self.inspect_executor = ThreadPoolExecutor(max_workers = inspect_threads)
        self.idb_executor = ThreadPoolExecutor(max_workers = idb_threads)
        self.bindiff_executor = ThreadPoolExecutor(max_workers = bindiff_threads)
        self.inspectors = [MagicInspector(), SizeInspector(), FileTypeInspector()]
        self.terminate = False
        self.threads = [start_thread(self.process_job) for _ in range(worker_threads)]
        self.bindiff = BindiffClient(urls = [BINDIFF_SERVICE_URL])
        self.whitelist = FuncDB(FUNCDB_SERVICE_URL)
        self.ida = IdaClient(urls = [IDA_SERVICE_URL]) 
Example 2
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: webCrawler.py    License: MIT License 6 votes vote down vote up
def main():
  print("Starting our Web Crawler")
  baseUrl = input("Website > ")
  numberOfThreads = input("No Threads > ")

  linksToCrawl = queue.Queue()
  urlLock = threading.Lock()
  linksToCrawl.put(baseUrl)
  haveVisited = []
  crawlers = []
  errorLinks = []

  for i in range(int(numberOfThreads)):
    crawler = Crawler(baseUrl, linksToCrawl, haveVisited, errorLinks, urlLock)
    crawler.start()
    crawlers.append(crawler)

  for crawler in crawlers:
    crawler.join()

  print("Total Number of Pages Visited {}".format(len(haveVisited)))
  print("Total Number of Pages with Errors {}".format(len(errorLinks))) 
Example 3
Project: smbprotocol   Author: jborean93   File: change_notify.py    License: MIT License 6 votes vote down vote up
def __init__(self, open):
        """
        A class that encapsulates a FileSystemWatcher over SMB. It is designed to make it easy to run the watcher in
        the background and provide an event that is fired when the server notifies that a change has occurred. It is
        up to the caller to action on that event through their own sync or asynchronous implementation.

        :param open: The Open() class of a directory to watch for change notifications.
        """
        self.open = open
        self.response_event = threading.Event()

        self._t_on_response = threading.Thread(target=self._on_response)
        self._t_on_response.daemon = True
        self._t_exc = None
        self._request = None
        self._file_actions = None
        self._result_lock = threading.Lock()  # Used to ensure the result is only processed once 
Example 4
Project: pysphere   Author: rnbguy   File: misphere.py    License: MIT License 6 votes vote down vote up
def send(self, msg_id, params=None, token=None):
        if msg_id not in self.session.locks:
            self.session.locks[msg_id] = threading.Lock()
        print("[LOCK] ", msg_id)
        if self.session.locks[msg_id].locked():
            return 0
        else:
            self.session.locks[msg_id].acquire()
        if token is None:
            token = self.session.token
        payload = self.create_payload(msg_id, token, params)
        data = json.dumps(payload).encode()
        print(data)
        timediff = time.time() - self.last_send
        if timediff < 0.5:
            time.sleep(0.5 - timediff)
        self.last_send = time.time()
        return self.socket.send(data) 
Example 5
Project: opentracing-python   Author: opentracing   File: tracer.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, scope_manager=None):
        """Initialize a MockTracer instance."""

        scope_manager = ThreadLocalScopeManager() \
            if scope_manager is None else scope_manager
        super(MockTracer, self).__init__(scope_manager)

        self._propagators = {}
        self._finished_spans = []
        self._spans_lock = Lock()

        # Simple-as-possible (consecutive for repeatability) id generation.
        self._next_id = 0
        self._next_id_lock = Lock()

        self._register_required_propagators() 
Example 6
Project: opentracing-python   Author: opentracing   File: span.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(
            self,
            tracer,
            operation_name=None,
            context=None,
            parent_id=None,
            tags=None,
            start_time=None):
        super(MockSpan, self).__init__(tracer, context)
        self._tracer = tracer
        self._lock = Lock()

        self.operation_name = operation_name
        self.start_time = start_time
        self.parent_id = parent_id
        self.tags = tags if tags is not None else {}
        self.finish_time = -1
        self.finished = False
        self.logs = [] 
Example 7
Project: Legobot   Author: Legobot   File: test_IRC.py    License: GNU General Public License v2.0 6 votes vote down vote up
def test_initialization():
        baseplate = Lego.start(None, threading.Lock())
        baseplate_proxy = baseplate.proxy()
        baseplate_proxy.add_child(IRC,  # nosec
                                  channels=['#foo'],
                                  nickname='test_nick',
                                  server='foo.testing',
                                  port=6667,
                                  use_ssl=False,
                                  username='test_username',
                                  password='test_password')
        # Cleanup
        children = baseplate_proxy.children.get()
        for child in children:
            child.stop()
        baseplate.stop() 
Example 8
Project: Legobot   Author: Legobot   File: Lego.py    License: GNU General Public License v2.0 6 votes vote down vote up
def __init__(
            self, baseplate, lock: threading.Lock, log_file=None, acl=None):
        """
        :param baseplate: the baseplate Lego, which should be \
                          the same instance of Lego for all Legos
        :param lock: a threading lock, which should be the same \
                     instance of threading.Lock for all Legos
        """
        super().__init__()
        if not lock:
            raise LegoError("Lock expected but not provided!")
        self.baseplate = baseplate
        self.children = []
        self.lock = lock
        self.log_file = log_file
        self.acl = acl 
Example 9
Project: Paradrop   Author: ParadropLabs   File: update_manager.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, reactor):
        self.reactor = reactor

        self.updateLock = threading.Lock()
        self.updateQueue = []

        # Map update_id -> update object.
        self.active_changes = {}

        # TODO: Ideally, load this from file so that change IDs are unique
        # across system reboots.
        self.next_change_id = 1

        ###########################################################################################
        # Launch the first update call, NOTE that you have to use callInThread!!
        # This happens because the perform_updates should run in its own thread,
        # it makes blocking calls and such... so if we *don't* use callInThread
        # then this function WILL BLOCK THE MAIN EVENT LOOP (ie. you cannot send any data)
        #
        # FIXME: It should be noted that calling updateLock.acquire(), e.g.
        # from add_update, also blocks the main thread.  Synchronizing access
        # to the work queue should be reworked.
        ###########################################################################################
        self.reactor.callInThread(self._perform_updates) 
Example 10
Project: lirpg   Author: Hwhitetooth   File: replay_buffer.py    License: MIT License 6 votes vote down vote up
def __init__(self, buffer_shapes, size_in_transitions, T, sample_transitions):
        """Creates a replay buffer.

        Args:
            buffer_shapes (dict of ints): the shape for all buffers that are used in the replay
                buffer
            size_in_transitions (int): the size of the buffer, measured in transitions
            T (int): the time horizon for episodes
            sample_transitions (function): a function that samples from the replay buffer
        """
        self.buffer_shapes = buffer_shapes
        self.size = size_in_transitions // T
        self.T = T
        self.sample_transitions = sample_transitions

        # self.buffers is {key: array(size_in_episodes x T or T+1 x dim_key)}
        self.buffers = {key: np.empty([self.size, *shape])
                        for key, shape in buffer_shapes.items()}

        # memory management
        self.current_size = 0
        self.n_transitions_stored = 0

        self.lock = threading.Lock() 
Example 11
Project: botbuilder-python   Author: microsoft   File: cosmosdb_partitioned_storage.py    License: MIT License 6 votes vote down vote up
def __init__(self, config: CosmosDbPartitionedConfig):
        """Create the storage object.

        :param config:
        """
        super(CosmosDbPartitionedStorage, self).__init__()
        self.config = config
        self.client = None
        self.database = None
        self.container = None
        self.compatability_mode_partition_key = False
        # Lock used for synchronizing container creation
        self.__lock = Lock()
        if config.key_suffix is None:
            config.key_suffix = ""
        if not config.key_suffix.__eq__(""):
            if config.compatibility_mode:
                raise Exception(
                    "compatibilityMode cannot be true while using a keySuffix."
                )
            suffix_escaped = CosmosDbKeyEscape.sanitize_key(config.key_suffix)
            if not suffix_escaped.__eq__(config.key_suffix):
                raise Exception(
                    f"Cannot use invalid Row Key characters: {config.key_suffix} in keySuffix."
                ) 
Example 12
Project: HardRLWithYoutube   Author: MaxSobolMark   File: replay_buffer.py    License: MIT License 6 votes vote down vote up
def __init__(self, buffer_shapes, size_in_transitions, T, sample_transitions):
        """Creates a replay buffer.

        Args:
            buffer_shapes (dict of ints): the shape for all buffers that are used in the replay
                buffer
            size_in_transitions (int): the size of the buffer, measured in transitions
            T (int): the time horizon for episodes
            sample_transitions (function): a function that samples from the replay buffer
        """
        self.buffer_shapes = buffer_shapes
        self.size = size_in_transitions // T
        self.T = T
        self.sample_transitions = sample_transitions

        # self.buffers is {key: array(size_in_episodes x T or T+1 x dim_key)}
        self.buffers = {key: np.empty([self.size, *shape])
                        for key, shape in buffer_shapes.items()}

        # memory management
        self.current_size = 0
        self.n_transitions_stored = 0

        self.lock = threading.Lock() 
Example 13
Project: rtp_cluster   Author: sippy   File: EventDispatcher.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def __init__(self, freq = 100.0):
        EventDispatcher2.state_lock.acquire()
        if EventDispatcher2.ed_inum != 0:
            EventDispatcher2.state_lock.release()
            raise StdException('BZZZT, EventDispatcher2 has to be singleton!')
        EventDispatcher2.ed_inum = 1
        EventDispatcher2.state_lock.release()
        self.tcbs_lock = Lock()
        self.tlisteners = []
        self.slisteners = []
        self.signals_pending = []
        self.last_ts = MonoTime()
        self.my_ident = get_ident()
        self.elp = ElPeriodic(freq)
        self.elp.CFT_enable(signal.SIGURG)
        self.bands = [(freq, 0),] 
Example 14
Project: jawfish   Author: war-and-code   File: queue.py    License: MIT License 6 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)
        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = _threading.Lock()
        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = _threading.Condition(self.mutex)
        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = _threading.Condition(self.mutex)
        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = _threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 15
Project: jawfish   Author: war-and-code   File: queue.py    License: MIT License 6 votes vote down vote up
def __init__(self, maxsize=0):
        self.maxsize = maxsize
        self._init(maxsize)

        # mutex must be held whenever the queue is mutating.  All methods
        # that acquire mutex must release it before returning.  mutex
        # is shared between the three conditions, so acquiring and
        # releasing the conditions also acquires and releases mutex.
        self.mutex = threading.Lock()

        # Notify not_empty whenever an item is added to the queue; a
        # thread waiting to get is notified then.
        self.not_empty = threading.Condition(self.mutex)

        # Notify not_full whenever an item is removed from the queue;
        # a thread waiting to put is notified then.
        self.not_full = threading.Condition(self.mutex)

        # Notify all_tasks_done whenever the number of unfinished tasks
        # drops to zero; thread waiting to join() is notified to resume
        self.all_tasks_done = threading.Condition(self.mutex)
        self.unfinished_tasks = 0 
Example 16
Project: python-esppy   Author: sassoftware   File: visuals.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self,visuals,datasource,**kwargs):
        Chart.__init__(self,visuals,datasource,**kwargs)
        self.layout.overflow = "auto"
        if self.hasOpt("image") == False:
            raise Exception("you must specify the image property")
        self._entries = {}
        self._lock = threading.Lock()
        self._imageWidth = self.getOpt("image_width",300)
        self._imageHeight = self.getOpt("image_height",300)
        orientation = self.getOpt("orientation","vertical")

        if orientation == "horizontal":
            self.layout.width = self.getOpt("width","800px")
            self.layout.height = str(self._imageHeight + 100) + "px"
        else:
            self.layout.width = str(self._imageWidth + 80) + "px"
            self.layout.height = self.getOpt("height","800px")

        self._detection = None 
Example 17
Project: pymumble   Author: Robert904   File: soundoutput.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, mumble_object, audio_per_packet, bandwidth):
        """
        audio_per_packet=packet audio duration in sec
        bandwidth=maximum total outgoing bandwidth
        """
        self.mumble_object = mumble_object
        
        self.Log = self.mumble_object.Log
        
        self.pcm = ""
        self.lock = threading.Lock()
        
        self.codec = None  # codec currently requested by the server
        self.encoder = None  # codec instance currently used to encode
        self.encoder_framesize = None  # size of an audio frame for the current codec (OPUS=audio_per_packet, CELT=0.01s)
        
        self.set_audio_per_packet(audio_per_packet)
        self.set_bandwidth(bandwidth)

        self.codec_type = None  # codec type number to be used in audio packets
        self.target = 0  # target is not implemented yet, so always 0
        
        self.sequence_start_time = 0  # time of sequence 1
        self.sequence_last_time = 0  # time of the last emitted packet
        self.sequence = 0  # current sequence 
Example 18
Project: pymumble   Author: Robert904   File: soundqueue.py    License: GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, mumble_object):
        self.mumble_object = mumble_object
        
        self.queue = deque()
        self.start_sequence = None
        self.start_time = None

        self.receive_sound = True
        
        self.lock = Lock()
        
        # to be sure, create every supported decoders for all users
        # sometime, clients still use a codec for a while after server request another...
        self.decoders = {
                    PYMUMBLE_AUDIO_TYPE_CELT_ALPHA: pycelt.CeltDecoder(PYMUMBLE_SAMPLERATE, 1, "0.7"),
                    PYMUMBLE_AUDIO_TYPE_CELT_BETA: pycelt.CeltDecoder(PYMUMBLE_SAMPLERATE, 1, "0.11"),
                    PYMUMBLE_AUDIO_TYPE_OPUS: pyopus.OpusDecoder(PYMUMBLE_SAMPLERATE, 1)                    
        } 
Example 19
Project: me-ica   Author: ME-ICA   File: scheduling.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def __init__(self, result_container=None, verbose=False):
        """Initialize the scheduler.

        result_container -- Instance of ResultContainer that is used to store
            the results (default is None, in which case a ListResultContainer
            is used).
        verbose -- If True then status messages will be printed to sys.stdout.
        """
        if result_container is None:
            result_container = OrderedResultContainer()
        self.result_container = result_container
        self.verbose = verbose
        self._n_open_tasks = 0  # number of tasks that are currently running
        # count the number of submitted tasks, also used for the task index
        self._task_counter = 0
        self._lock = threading.Lock()
        self._last_callable = None  # last callable is stored
        # task index of the _last_callable, can be *.5 if updated between tasks
        self._last_callable_index = -1.0

    ## public read only properties ## 
Example 20
Project: incubator-spot   Author: apache   File: hdfs_client.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, hdfs_path, nbytes):
        self._data = {}
        self._lock = Lock()
        self._hpath = hdfs_path
        self._nbytes = nbytes 
Example 21
Project: incubator-spot   Author: apache   File: hdfs_client.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, hdfs_path, nbytes):
        self._data = {}
        self._lock = Lock()
        self._hpath = hdfs_path
        self._nbytes = nbytes 
Example 22
Project: BASS   Author: Cisco-Talos   File: core.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, job_id):
        self.samples = []
        self.status = self.STATUS_CREATED
        self.thread = None
        self.finished = Event()
        self.status_lock = Lock()
        self.id = job_id 
Example 23
Project: BiblioPixelAnimations   Author: ManiacalLabs   File: system_eq.py    License: MIT License 5 votes vote down vote up
def __init__(self, rate=8000, chunksize=128):
        self.rate = rate
        self.chunksize = chunksize
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=pyaudio.paInt16,
                                  channels=1,
                                  rate=self.rate,
                                  input=True,
                                  frames_per_buffer=self.chunksize,
                                  stream_callback=self.new_frame)
        self.lock = threading.Lock()
        self.stop = False
        self.frames = [] 
Example 24
Project: BiblioPixelAnimations   Author: ManiacalLabs   File: system_eq.py    License: MIT License 5 votes vote down vote up
def __init__(self, rate=8000, chunksize=128):
        self.rate = rate
        self.chunksize = chunksize
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=pyaudio.paInt16,
                                  channels=1,
                                  rate=self.rate,
                                  input=True,
                                  frames_per_buffer=self.chunksize,
                                  stream_callback=self.new_frame)
        self.lock = threading.Lock()
        self.stop = False
        self.frames = [] 
Example 25
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: rlockVLock.py    License: MIT License 5 votes vote down vote up
def __init__(self):
    self.a = 1
    self.b = 2
    self.lock = threading.Lock() 
Example 26
Project: Learning-Concurrency-in-Python   Author: PacktPublishing   File: rlockVLock.py    License: MIT License 5 votes vote down vote up
def modifyB(self):
    print("Modifying B : Lock Acquired: {}".format(self.lock._is_owned()))
    print("{}".format(self.lock))
    self.b = self.b - 1
    time.sleep(5) 
Example 27
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: operator.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self.ref_holder = {}
        self.counter = 0
        self.result_deps = set()
        self.lock = Lock() 
Example 28
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: autograd.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self):
            self.ref_holder = {}
            self.counter = 0
            self.lock = Lock() 
Example 29
Project: smbprotocol   Author: jborean93   File: connection.py    License: MIT License 5 votes vote down vote up
def __init__(self, message, message_type, connection, session_id=None):
        """
        [MS-SMB2] v53.0 2017-09-15

        3.2.1.7 Per Pending Request
        For each request that was sent to the server and is await a response
        :param message: The message to be sent in the request
        :param message_type: The type of message that is set in the header's data field.
        :param connection: The Connection the request was sent under.
        :param session_id: The Session Id the request was for.
        """
        self.async_id = None
        self.message = message
        self.timestamp = datetime.now()
        self.cancelled = False

        # Used to contain the corresponding response from the server as the receiving in done in a separate thread.
        self.response = None

        # Used by the recv processing thread to say the response has been received and is ready for consumption.
        self.response_event = threading.Event()

        # Used to lock the request when the main thread is processing the PENDING result in case the background thread
        # receives the final result and fires the event before main clears it.
        self.response_event_lock = threading.Lock()

        # Stores the message_ids of related messages that are sent in a compound request. This is only set on the 1st
        # message in the request. Used when STATUS_STOPPED_ON_SYMLINK is set and we need to send the whole compound
        # request again with the new path.
        self.related_ids = []

        # Cannot rely on the message values as it could be a related compound msg which does not set these values.
        self.session_id = session_id

        self._connection = connection
        self._message_type = message_type  # Used to rehydrate the message data in case it's needed again. 
Example 30
Project: facebook-wda   Author: openatx   File: __init__.py    License: MIT License 5 votes vote down vote up
def namedlock(name):
    """
    Returns:
        threading.Lock
    """
    if not hasattr(namedlock, 'locks'):
        namedlock.locks = defaultdict(threading.Lock)
    return namedlock.locks[name]