python-multithreading

How to send a signal to the main thread in python without using join?

醉酒当歌 提交于 2019-12-12 15:28:40
问题 I Am trying to send a signal from a child thread to the main thread in a multi-threaded program (cannot use multi-processes). Unfortunately even after exhausting all the reading materials available online (which I could find), I Am unable to get a clear idea of how to do so. I Am a beginner to signals AND to python so please bear with me and explain as you would to a novice. I cannot use the join method in the process, since I want both the threads to be running simultaneously. Here is the

Multi-threaded Python Web Crawler Got Stuck

故事扮演 提交于 2019-12-12 15:18:57
问题 I'm writing a Python web crawler and I want to make it multi-threaded. Now I have finished the basic part, below is what it does: a thread gets a url from the queue; the thread extracts the links from the page, checks if the links exist in a pool (a set), and puts the new links to the queue and the pool; the thread writes the url and the http response to a csv file. But when I run the crawler, it always gets stuck eventually, not exiting properly. I have gone through the official document of

Python: 'thread._local object has no attribute 'todo'

梦想的初衷 提交于 2019-12-12 12:29:51
问题 I am currently programming a python-based datagram-Server using threads and all of that. I encountered the following problem: I am using multiple Allocation Threads to allocate incoming packages to the different processing threads. Inside the Processing Threads, I am using threading.local() to keep track of thread-local variables. I am currently testing how my server reacts during high load (2000 Packets in ~2 seconds), and have come upon a strange behaviour of the local()-Object. It seems

Forcing a thread to block all other threads from executing

三世轮回 提交于 2019-12-12 10:32:48
问题 UPDATE: This answer states that what I'm trying to do is impossible as of April 2013. This, however, seems to contradict what Alex Martelli says in Python Cookbook (p. 624, 3rd ed.): Upon return, PyGILState_Ensure() always guarantees that the calling thread has exclusive access to the Python interpreter. This is true even if the calling C code is running a different thread that is unknown to the interpreter. The docs also seem to suggest GIL can be acquired, which would give me hope (except I

Python multiprocessing: synchronizing file-like object

浪尽此生 提交于 2019-12-12 07:58:40
问题 I'm trying to make a file like object which is meant to be assigned to sys.stdout/sys.stderr during testing to provide deterministic output. It's not meant to be fast, just reliable. What I have so far almost works, but I need some help getting rid of the last few edge-case errors. Here is my current implementation. try: from cStringIO import StringIO except ImportError: from StringIO import StringIO from os import getpid class MultiProcessFile(object): """ helper for testing multiprocessing

multiprocessing/threading: data appending & output return

余生颓废 提交于 2019-12-12 05:26:13
问题 I have a lengthy function called run below that contains a few instances of appending data. from multiprocessing import Process data = [] def run(): global data ... data.append(trace) ... if __name__ == '__main__': jobs = [] gen_count = 0 leaked_count = 0 system_count = 0 N = 100 for i in range(N): p = Process(target=run) jobs.append(p) p.start() However, using multiprocessing no data is appended. In addition, the function run returns several values that need to be added to gen_count , leaked

Triggering an exception in a main thread from a daemon thread in Python

余生颓废 提交于 2019-12-12 05:11:12
问题 How do you trigger an exception in a main thread, running blocking code, from a daemon thread in Python? I have a daemon thread that periodically checks a database for a record indicating that its current process should terminate (i.e. the user has pressed the "cancel" button). In my main non-daemon thread, I'm running a blocking call to an external process that I can't easily modify to gracefully terminate. However, the main thread can gracefully handle a normal KeyboardInterrupt when I

Thread seems to be blocking the process

耗尽温柔 提交于 2019-12-12 04:56:57
问题 class MyClass(): def __init__(self): ... def start(self): colorThread = threading.Thread(target = self.colorIndicator()) colorThread.start() while True: print ('something') ... ... I also have a print statement inside the colorIndicator() . That statement is getting printed. But the print statement inside the while loop of start() method isn't displayed on screen. The colorIndicator() also has an infinite loop. It gets some data from the internet and updates a counter. This counter is

New to Python Threading - It doesn't appear to make a difference

谁说胖子不能爱 提交于 2019-12-12 04:16:15
问题 I've cobbled together an automatic vnc scanner. It cycles through IP addresses and if it detects an open port 5900 it attempts a screen shot. It isn't pretty, and poorly constructed but it works. It is however slow. I've tried threading the processes but I've been struggling. You can see I've added in a timer so I can see how long it takes to scan 30 ip's. I've tried multiple types of threading and threading libraries. This current iteration is probably the fastest I've had it, but it is only

Real-time capture and processing of keypresses (e.g. keypress event)

走远了吗. 提交于 2019-12-12 03:57:23
问题 Note: I want to do this without using any external packages, like PyGame, etc. I am attempting to capture individual keypresses as they arrive and perform an action for specific characters, whether I simply want to "re-echo" the character, or not display it at all and do something else. I have found a cross-platform (though not sure about OS X) getch() implementation because I do not want to read a whole line like input() does: # http://code.activestate.com/recipes/134892/ def getch(): try: