locking

How to make a Python threaded program (with Locks) to run on multi-process?

How to make a Python threaded program (with Locks) to run on multi-process? Question: I have a multi-threaded program, and I want to let the user choose how to run them, either in serial, multi threads or multi cores, at least at the top level. A runnable demo is shown below illustrating my program’s logic. …

Total answers: 3

Release redis lock if process die

Release redis lock if process die Question: I’m using redis distributed locks to make sure some celery tasks don’t run concurrently. For managing locks with redis I’m using the python redis library version 4.0.0 (which specifies that any deadlock problems should be handled by the programmer). Since I would like to avoid using the timeout …

Total answers: 1

Share RLock between multiple instances of Python

Share RLock between multiple instances of Python with multiprocessing Question: Consider this MWE: from multiprocessing import RLock class TheSetup: def __init__(self): self._hardware_lock = RLock() def hold_hardware(self): return self._hardware_lock def do_thing_with_hardware(self): with self._hardware_lock: print(‘Doing thing…’) def do_other_thing_with_hardware(self): with self._hardware_lock: print(‘Doing other thing…’) if __name__==’__main__’: from multiprocessing.managers import BaseManager class TheSetupManager(BaseManager): pass the_setup = TheSetup() TheSetupManager.register(‘get_the_setup’, callable=lambda:the_setup) …

Total answers: 2

Does "select_for_update()" work with the update method in Django?

Does "select_for_update()" work with the update method in Django? Question: The documentation for Django 2.2, which I’m using, gives the following example usage for select_for_update: from django.db import transaction entries = Entry.objects.select_for_update().filter(author=request.user) with transaction.atomic(): for entry in entries: … Using this approach, one would presumably mutate the model instances assigned to entry and call save …

Total answers: 2

Python sharing a lock between processes

Python sharing a lock between processes Question: I am attempting to use a partial function so that pool.map() can target a function that has more than one parameter (in this case a Lock() object). Here is example code (taken from an answer to a previous question of mine): from functools import partial def target(lock, iterable_item): …

Total answers: 2

What is the difference between Lock and RLock

What is the difference between Lock and RLock Question: From the docs: threading.RLock() — A factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread …

Total answers: 3

Understanding Multiprocessing: Shared Memory Management, Locks and Queues in Python

Understanding Multiprocessing: Shared Memory Management, Locks and Queues in Python Question: Multiprocessing is a powerful tool in python, and I want to understand it more in depth. I want to know when to use regular Locks and Queues and when to use a multiprocessing Manager to share these among all processes. I came up with …

Total answers: 1

When and how to use Python's RLock

When and how to use Python's RLock Question: Reading through the Python docs I came across RLock. Can someone explain to me (with example) a scenario in which RLock would be preferred to Lock? With particular reference to: RLock‘s “recursion level”. How is this useful? A threads “ownership” of an RLock object Performance? Asked By: …

Total answers: 3

Preventing a class from direct instantiation in Python

Preventing a class from direct instantiation in Python Question: I have a super class with a method that calls other methods that are only defined in its sub classes. That’s why, when I create an instance of my super class and call its method, it cannot find the method and raises an error. Here is …

Total answers: 4

Python Conditional "With" Lock Design

Python Conditional "With" Lock Design Question: I am trying to do some shared locking using with statements def someMethod(self, hasLock = False): with self.my_lock: self.somethingElse(hasLock=True) def somethingElse(self, hasLock = False): #I want this to be conditional… with self.my_lock: print ‘i hate hello worlds” That make sense? I basically only want to do the with if …

Total answers: 5