python-multithreading

Ending non-daemon threads when shutting down an interactive python session

爷,独闯天下 提交于 2019-12-23 01:58:16
问题 Please consider the code below: #! /usr/bin/env python3 import threading import time class MyThread(threading.Thread): def __init__(self): super().__init__() self._quit_flag = False def run(self): while not self._quit_flag: print("Thread is alive!") time.sleep(0.500) def request_quit(self): self._quit_flag = True mt = MyThread() mt.start() After saving this as 'test.py' and running 'python3 -i test.py', I get an interactive session, where the thread regularly prints the "Thread is alive!"

Why doesn't concurrent.futures.ThreadPoolExecutor().submit return immediately?

元气小坏坏 提交于 2019-12-22 19:45:12
问题 In this code: import concurrent.futures import time def pafter(t): time.sleep(t) print('Hi') with concurrent.futures.ThreadPoolExecutor(5) as e: e.submit(pafter, 2) print('With returned') I expect to see: With returned Hi but I see: Hi With returned Why doesn't submit return immediately? What do I change to make it do so? 回答1: Using the with statement is equivalent to calling executor.shutdown() , which is documented like this: shutdown(wait=True) Signal the executor that it should free any

Why doesn't concurrent.futures.ThreadPoolExecutor().submit return immediately?

旧巷老猫 提交于 2019-12-22 19:45:11
问题 In this code: import concurrent.futures import time def pafter(t): time.sleep(t) print('Hi') with concurrent.futures.ThreadPoolExecutor(5) as e: e.submit(pafter, 2) print('With returned') I expect to see: With returned Hi but I see: Hi With returned Why doesn't submit return immediately? What do I change to make it do so? 回答1: Using the with statement is equivalent to calling executor.shutdown() , which is documented like this: shutdown(wait=True) Signal the executor that it should free any

bottle gevent and threading: gevent is only usable from a single thread

前提是你 提交于 2019-12-22 09:39:37
问题 I have a python bottle application, which uses threads. due to the fact I'm using monkey.patch, the threads were blocking app execution (a dialog box fired from a thread was blocking bottle routes from responding to the client, until dismissed.) A little research here showed I should use monkey patch without trying to patch Thread: # Patch python's threads with greenlets from gevent import monkey monkey.patch_all(thread=False) This does not block on a minimal example I wrote. But raises these

recursion max error when when using futures.ProcessPoolExecutor but not futures.ThreadPoolExecutor with PRAW wrapper

微笑、不失礼 提交于 2019-12-22 09:17:14
问题 I am using this code to scrape an API: submissions = get_submissions(1) with futures.ProcessPoolExecutor(max_workers=4) as executor: #or using this: with futures.ThreadPoolExecutor(max_workers=4) as executor: for s in executor.map(map_func, submissions): collection_front.update({"time_recorded":time_recorded}, {'$push':{"thread_list":s}}, upsert=True) It works great/fast with threads but when I try to use processes I get a full queue and this error: File "/usr/local/lib/python3.4/dist

Limiting the number of processes running at a time from a Python script

大城市里の小女人 提交于 2019-12-22 08:46:41
问题 I'm running a backup script that launches child processes to perform backups by rsync. However I have no way to limit the number of rsyncs it launches at a time. Here's the code I'm working on at the moment: print "active_children: ", multiprocessing.active_children() print "active_children len: ", len(multiprocessing.active_children()) while len(multiprocessing.active_children()) > 49: sleep(2) p = multiprocessing.Process(target=do_backup, args=(shash["NAME"],ip,shash["buTYPE"], )) jobs

python: tkinter to display video from webcam and do a QR scan

安稳与你 提交于 2019-12-22 04:08:04
问题 I have been trying to create a tkinter top level window that streams video form webcam and do a QR scan. I got this QR scan code from SO and another code that just updates images from webcam instead of streaming the video on a tkinter label. and i tried to combine these both so that a toplevel window with a label updating image from webcam and a close button to close the toplevel window. And while it streams the images, it can scan for QR code and if a scan is successful, the webcam and the

Parallelize this nested for loop in python

断了今生、忘了曾经 提交于 2019-12-21 16:21:35
问题 I'm struggling again to improve the execution time of this piece of code. Since the calculations are really time-consuming I think that the best solution would be to parallelize the code. I was first working with maps as explained in this question, but then I tried a more simple approach thinking that I could find a better solution. However I couldn't come up with anything yet, so since it's a different problem I decided to post it as a new question. I am working on a Windows platform, using

To execute a function every x minutes: sched or threading.Timer?

限于喜欢 提交于 2019-12-21 10:16:00
问题 I need to program the execution of a give method every x minutes. I found two ways to do it: the first is using the sched module, and the second is using Threading.Timer . First method : import sched, time s = sched.scheduler(time.time, time.sleep) def do_something(sc): print "Doing stuff..." # do your stuff sc.enter(60, 1, do_something, (sc,)) s.enter(60, 1, do_something, (s,)) s.run() The second : import threading def do_something(sc): print "Doing stuff..." # do your stuff t = threading

Using Multithreaded queue in python the correct way?

被刻印的时光 ゝ 提交于 2019-12-21 07:23:21
问题 I am trying to use The Queue in python which will be multithreaded. I just wanted to know the approach I am using is correct or not. And if I am doing something redundant or If there is a better approach that I should use. I am trying to get new requests from a table and schedule them using some logic to perform some operation like running a query. So here from the main thread I spawn a separate thread for the queue. if __name__=='__main__': request_queue = SetQueue(maxsize=-1) worker =