FAQ Database Discussion Community


How to collect output from a Python subprocess

python,subprocess,stanford-nlp,python-multithreading
I am trying to make a python process that reads some input, processes it and prints out the result. The processing is done by a subprocess (Stanford's NER), for ilustration I will use 'cat'. I don't know exactly how much output NER will give, so I use run a separate...

how to run two while loops at the same time in Python?

python,parallel-processing,python-multithreading
I am new to Python and using Zelle's graphics to create a game. I need the two while loops below to run at the same time, however I am running into difficulty. I tried nesting the while loops but then the horses and civilians would only move if the mouse...

Python threading how get output of multiprocess in sequence

python-multithreading
I am using Python threading to process multiple files and I want output of each of the files 0 through 100 being processed to be written in a file in orderly fashion Currently I am saving output of all the thread as they get executed and hence the order is...

Killing multiple httpservers running on different ports

python,multithreading,python-multithreading,socketserver
I start multiple servers using the following: from threading import Thread from SocketServer import ThreadingMixIn from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/plain") self.end_headers() self.wfile.write("Hello World!") class ThreadingHTTPServer(ThreadingMixIn, HTTPServer): pass def serve_on_port(port): server = ThreadingHTTPServer(("localhost",port), Handler) server.serve_forever()...

How to know if threading.Condition.wait(timeout) has timed out or has been notified?

python,multithreading,python-multithreading
I'm developing an application with some threads, each one running an infinite loop with a time sleep. What I want is to finish all threads once the main one has finished, here an example: def main(): display_res_stop = threading.Condition() display_result_t = threading.Thread(target=sample_t, args=(display_res_stop, )) display_result_t.start() time.sleep(4) display_res_stop.acquire() display_res_stop.notify() display_res_stop.release() def...

Open audio with callback using ALSA

python,linux,audio,python-multithreading,alsa
Is it possible in Python, using ALSA, to access the audio hardware for playback, with a callback function: def audiocallback(): # create some audio and return a buffer of 1024 samples (~23 ms @ 44.1khz) # that is going to be played on the device return buffer openaudio(deviceid=1, type=OUTPUT, freq=44100,...

Calling While loop Function in __main__?

python,function,while-loop,python-multithreading
Here I have a program which polls the queue for an event, if found it executes an order to a REST API. In addition if an event is found, it prints the current price that I need to use as my stopLoss. This code runs exactly as I would like...

Time management for real-time applications

python,sleep,python-multithreading
How to generate precise pulses with python? from threading import Thread import time def runA(): while True: print "a" sendSignal(1) time.sleep(0.0001) sendSignal(0) time.sleep(0.005) if __name__ == "__main__": t1 = Thread(target = runA) t1.setDaemon(True) t1.start() while True: sleep(10) time management in python is not precise. specially to create multi-threaded PWM generators...

Python NamedTemporaryFile deleted without closing it

python,python-multithreading,temporary-files
I'm working with ZOPE and Python 2.4 and I have a problem according to a NamedTemporaryFile. I read that the file is deleted when it is closed, but somehow my file is deleted even if it's not. I have a function that writes some xml specifications to a file with...

Threading script not printing to console

python,stdout,python-multithreading
this is really baffling me, and I cannot find the answer. import thread import time import random import sys sys.stdout.flush() def run_often(thread_name, sleep_time): while True: time.sleep(sleep_time) print "%s" % (thread_name) def run_randomly(thread_name, sleep_time): while True: time.sleep(sleep_time) print "%s" % (thread_name) thread.start_new_thread(run_often,("Often runs", 2)) thread.start_new_thread(run_randomly, ("Fast and random", random.random())) the problem...

Make python socket asynchronous?

python,asynchronous,python-multithreading,asyncore
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...

QThread use in Python

python,multithreading,python-multithreading
I am fairly new in writing bigger programs in Python (I was only writing short scripts before). The program I'm about to write recives data from an external device, saves it to database and displays it in GUI (continuously). Since handling the data is time consuming I want to use...

Return a variable's value from a method of the constructor to another method using a thread daemon

python,python-multithreading
I have this part of a script in python: class Filtro: def __init__(self,cmd): def exec_cmd(): proc = subprocess.Popen([cmd, '-'], stdin=subprocess.PIPE, ) return proc self.thr=threading.Thread(name="Demone_cmd",target=exec_cmd) self.thr.setDaemon(True) self.proc=self.thr.start() def inp(self,txt): f=open(txt,"r") self.proc.communicate(f.read()) f.close() filtro=Filtro(sys.argv[1]) filtro.inp(sys.argv[2]) I want the return value of exec_cmd -- i.e., proc -- in the method inp, but the...

Which EvtHandler to use for PostEvent in wxPython

python,python-2.7,wxpython,python-multithreading
This question addresses the very specific question of the EvtHandler required to post en event using wxPython. Background I'm using Python 2.7. In the example below I have two kinds of events: StartMeausuringEvent is triggered within a wx.Panel derived object (DisplayPanel), hence I use self.GetEventHandler(), this works, even though the...

Stopping SocketServer with blocking handle

python,python-multithreading,tcpserver
I'm using SocketServer.ThreadingMixIn, pretty much as in the docs. Other than having extracted the clients to run on their own script, I've also redefined the handle method as I want the connection to the client to keep alive and receive more messages: def handle(self): try: while True: data = self.request.recv(1024)...

What can be slowing down my program when i use multithreading?

python-3.x,python-multithreading
I'm writing a program that downloads data from a website (eve-central.com). It returns xml when I send a GET request with some parameters. The problem is that I need to make about 7080 of such requests because i can't specify the typeid parameter more than once. def get_data_eve_central(typeids, system, hours,...

Why aren't my consumers consuming?

python,multiprocessing,python-multithreading
EDIT: I have tracked down the problem to be the part of my program where I download the Zip file and parse it. If I comment that out and replace it with a default line, it parses 10,000 times with no problem. Not sure how much of this question should...

What is Lock in Python _thread module?

python,multithreading,python-multithreading
I read "Core Python Applications Programming", in which this code is written. import _thread from time import sleep, ctime loops = [4, 2] def loop(nloop, nsec, lock): print("start loop", nloop, "at:", ctime()) sleep(nsec) print("loop", nloop, "done at:", ctime()) lock.release() def main(): print("starting at:", ctime()) locks = [] nloops = range(len(loops))...

Which operations in the Sublime Text 2 API are thread-safe?

multithreading,sublimetext2,sublimetext,python-multithreading,sublime-text-plugin
In the Sublime Text 3 API, all operations are threadsafe: Threading All API functions are thread-safe ... but in the Sublime Text 2 API, this is not the case. As I've personally learned to my displeasure, some operations will, in an unpredictable and OS-dependent way, fail when called from threads...