python-multithreading

Input without stopping program

放肆的年华 提交于 2019-12-12 03:53:44
问题 I am trying to make a countdown timer that prints the time remaining, and when you input something, it prints what you inputted. My problem is I don't want to wait for the input, just keep running the timer. My incorrect code: timer = 100 while True: print(timer) timer -= 1 if input('> '): print('the output of input') You could say I want to have the timer printing the time in the background. 回答1: Here's a function that will timeout if no input is given: import select import sys def timeout

What's the correct pattern for threading in a python qt5 application?

烂漫一生 提交于 2019-12-12 01:54:26
问题 I'm trying to write a pyqt5 application with a long running, but not CPU intensive process. I'd like to be able to run it without hanging the UI, so I'm trying to use threading, but since it doesn't seem like I can just run a thread and have it stop after its gone through its code so that it can be run again, I've tried setting up the thread to wait for a variable to change before running. I know this can't be the correct pattern for running long processes in a pyqt app. import time import

Multithreading with Tkinter

雨燕双飞 提交于 2019-12-11 21:03:28
问题 I'm having some issues with a Tkinter-based GUI. Basically the GUI creates lots of threads and run them. When each thread has finished, I'd like it to update a label to inform the user of this specific thread completion. I know Tkinter widgets are not thread-safe and that it is a bad practice to allow subthreads to update the view. So I'm trying to trigger an event on the main thread so it can update the view itself. I'm running the simplified code sample below: from Tkinter import * from

Threading not working

痞子三分冷 提交于 2019-12-11 20:38:18
问题 I am trying to get this little file copy application working which shows a progress bar, but I am not understanding why this won't work, as it locks up the gui whilst updating the gauge. import shutil import os import threading import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.source = os.path.expanduser("~/Desktop/FolderToCopy") self.destination = os.path.expanduser("~/Desktop/BackupFolder/Temp") panel = wx.Panel(self,

Python Multiprocessing Start Process in Module Other Than Main

久未见 提交于 2019-12-11 15:54:58
问题 I have three modules, worker , master , and MainTests . I'm running the MainTests module as the main script. In MainTests , I call master.run() , inside of which I need to spawn multiple worker processes. Is this possible? In all the python multiprocessing tutorials I have come across, processes are started in the main module. If this is possible, could someone provide an example as to what this might look like? This is what I have attempted so far: Worker.py import time class Worker(object):

Python Threading - Creation of a subclass?

人盡茶涼 提交于 2019-12-11 12:34:51
问题 I am having a problem wrapping my brain around the reason for creating a subclass when using threading in python. I've read a number of websites including tutorialspoint. The docs say you need to define a new subclass of the Thread class. I have a basic understanding of classes but haven't played with subclasses at all. I haven't had to do anything like this yet with any other modules I've used like os & ftplib. Can anyone point me to a site that may explain this better for a newbie scripter?

Make python socket asynchronous?

我只是一个虾纸丫 提交于 2019-12-11 10:02:16
问题 I have the following script and I'm trying to make it asynchronous. It works fine as a client to connect through TCP and send the text file. However, I'm executing it from another software program, and it is slowing down the program I'm running it from (hence the need for async). My aim is to get the script to run in it's own background thread, so that it doesnt affect the program it is being executed from. I've tried adding asyncore, but I just keep getting errors and it wont seem to connect

How to run a thread more than once in python

人盡茶涼 提交于 2019-12-11 09:39:34
问题 I am trying to run a thread more than once and keep getting an error: RuntimeError: threads can only be started once I have tried reading up multithreading and implementing it in my code without any luck. Here is the function I am threading: def receive(q): host = "" port = 13000 buf = 1024 addr = (host,port) Sock = socket(AF_INET, SOCK_DGRAM) Sock.bind(addr) (data, addr) = Sock.recvfrom(buf) q.put(data) Here is the code I want to run: q = Queue.Queue() r = threading.Thread(target=receive,

How to use PostgreSQL in multi thread python program

半城伤御伤魂 提交于 2019-12-11 08:35:47
问题 I am using psycopg2 (2.6) connect to PostgreSQL database in a multi-threading python program. When queue size in program increase, select queries get error "no results to fetch", but inserts records to db works very well. example code: class Decoders(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): self.decode() def decode(self): queue = self.queue db = Database() while queue.qsize() > 0: # calling db methods, just an example temp

Cancel timer in Python

北慕城南 提交于 2019-12-11 08:05:19
问题 I am working on timer class in python and wrote a simple test code for the same. My purpose is to print the "hello world" message 10 times and then cancel the timer once the iterations are done. The problem is I am unable to cancel the timer and code seems to print "hello world" infinitely. Below is my code: from threading import Timer class myclass(): iteration_count = 0 heartbeat = 1 def printMsg(self): print "hello world!" def start_job(self): self.printMsg() self.iteration_count = self