thread-safety

Calling asyncio.run_coroutine_threadsafe across different event loops

Calling asyncio.run_coroutine_threadsafe across different event loops Question: I have a class inside a microservice that looks like this: import asyncio import threading class A: def __init__(self): self.state = [] self._flush_thread = self._start_flush() self.tasks = set() def _start_flush(self): threading.Thread(target=self._submit_flush).start() def _submit_flush(self): self._thread_loop = asyncio.new_event_loop() self._thread_loop.run_until_complete(self.flush_state()) # async def regular_func(self): # This function is called on an …

Total answers: 2

Twisted Tkinter Manual Data Input Not Working

Twisted Tkinter Manual Data Input Not Working Question: I’m working on an application for use in a reception/office environment that allows for the sending of notices to specific offices/computers or in my case for testing at the moment globally to all connected clients. The issue I have faced is that the server claims to send …

Total answers: 1

Getting user input while a timer is counting and updating the console

Getting user input while a timer is counting and updating the console Question: I have a count-up/count-down timer library, and I’ve written some demo code that kicks off an instance of its main class, and updates the console as it counts down. When the timer expires, it displays a message to that effect. The demo …

Total answers: 1

Is extending a Python list (e.g. l += [1]) guaranteed to be thread-safe?

Is extending a Python list (e.g. l += [1]) guaranteed to be thread-safe? Question: If I have an integer i, it is not safe to do i += 1 on multiple threads: >>> i = 0 >>> def increment_i(): … global i … for j in range(1000): i += 1 … >>> threads = [threading.Thread(target=increment_i) …

Total answers: 2

Are global variables thread-safe in Flask? How do I share data between requests?

Are global variables thread-safe in Flask? How do I share data between requests? Question: In my application, the state of a common object is changed by making requests, and the response depends on the state. class SomeObj(): def __init__(self, param): self.param = param def query(self): self.param += 1 return self.param global_obj = SomeObj(0) @app.route(‘/’) def …

Total answers: 4

Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread

Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread Question: Stack overflow. Once again, I come to you in a time of dire need, teetering precariously on the brink of insanity. This question – as may be evident from the title – is an amalgamation of several other questions I have seen …

Total answers: 1

Are Python instance variables thread-safe?

Are Python instance variables thread-safe? Question: OK, check following codes first: class DemoClass(): def __init__(self): #### I really want to know if self.Counter is thread-safe. self.Counter = 0 def Increase(self): self.Counter = self.Counter + 1 def Decrease(self): self.Counter = self.Counter – 1 def DoThis(self): while True: Do something if A happens: self.Increase() else: self.Decrease() time.sleep(randomSecs) …

Total answers: 5

Thread Safety in Python's dictionary

Thread Safety in Python's dictionary Question: I have a class which holds a dictionary class OrderBook: orders = {‘Restaurant1’: None, ‘Restaurant2’: None, ‘Restaurant3’: None, ‘Restaurant4’: None} @staticmethod def addOrder(restaurant_name, orders): OrderBook.orders[restaurant_name] = orders And I am running 4 threads (one for each restaurant) that call the method OrderBook.addOrder. Here is the function ran by each …

Total answers: 5

Are Python built-in containers thread-safe?

Are Python built-in containers thread-safe? Question: I would like to know if the Python built-in containers (list, vector, set…) are thread-safe? Or do I need to implement a locking/unlocking environment for my shared variable? Asked By: Phong || Source Answers: They are thread-safe as long as you don’t disable the GIL in C code for …

Total answers: 4