I had an interview question last time about lockable trees in which a node of a tree can be locked if its ancestors are not locked and also its children are not locked. The problem I have now is that I have to make this program thread-safe. I used so many concepts but the interviewer ..
I have a request_processor that processes web requests coming to my python webserver. However, this request_processor needs to be updated every x hours. I am using schedule library in python for the same. I want to make sure I am not leaving any holes that compromises thread safety. Please review the following approach that I ..
I’m writing code that I want to be usable in interactive shell, like IPython, that means that the code needs to be able to handle a completely asynchronous, and unexpected, exception like KeyboardInterrupt and still be functional after it. I guess I should use context managers to handle locking and unlocking. Problem is, what if ..
I looked at several retry solutions, such as retry and retrying libraries, but all of them use time.sleep which blocks a thread. I’m trying to understand whether there is an option to implement a retry mechanism in Python, that doesn’t block a thread. I understand that I can create an additional thread on each retry, ..
I have two or more threads instanciated from same class called ‘Player’, sharing memory_buffer. class Player(Thread): def __init__(self, player_number, memory_buffer): super().__init__() self.memory_buffer = memory_buffer self.player_number = player_number self.lock = Lock() def add_data(self, count): with self.lock: self.memory_buffer.add_data(self.player_number, count) def run(self): count = 0 while True: self.add_data(count) count += 1 Where memory_buffer is an object as below: ..
I am struggling with a problem that should b easy but revealed difficult. I want to set up a websocket server that broadcasts messages to every clients. The messages comes from another API that requires an infinite loop that proceeds events. I decided to put this infinite loop in another thread but I quite struggle ..
I have a module called myLog.py which is being accessed by multiple other modules in a project. The myLog.py module has two handlers: file_handler that inputs logs into file and stream_handler that outputs logs to a console. For modules where no threading is occurring i.e myLog.py is only being accessed by a single process the ..
I was wondering if there was a way to run the tkinter mainloop on a separate thread (not the mainthread) so that the terminal is "free". Let’s say we have a simple GUI: import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.grid() self.createWidgets() def printHello(self): print("Hello") def createWidgets(self): self.quitButton = tk.Button(self, text=’Quit’, ..
I am calculating a measure above a graph, using sequential for every operation takes around 40 seconds, then I need to do this calculation 38 times, a total of 1520 sec or 25min. So, I thought if I use threads this can be faster. I have this template for using threads but I was checking ..
I’m writing a multiprocessing program which involves n number of processes filling m number of queues which are shared between them. The contents of each queue then needs to be written to a file corresponding to the queue and this is causing me some problems. I didn’t think this was a very hard problem so ..