python-multithreading

How to control a Thread via input()?

南笙酒味 提交于 2020-01-15 23:04:28
问题 I want to run a code with process parallel to my main code but also want to access its parameters or start/stop the process via command prompt. my machine is win7 64bit. Something in mind is: from multiprocessing import Process class dllapi(): ... def apiloop(params, args): apiclient = dllapi(**args) while True: apiclient.cycle() params = [....] def mainloop(args): p = Process(target = apiloop, args=(params, args, )) while True: cmd = input() if cmd == 'kill': p.terminate() if cmd == 'stop':

Pool only executes a single thread instead of 4, and how do I make it infinite?

大憨熊 提交于 2020-01-15 11:54:08
问题 So I am working on a little Python tool to stress test an API of application. I've got a pretty nice script using Threading, but then I read that it will require manual coding to maintain n number of concurrent threads (meaning, starting new ones as soon as old ones finish), and the suggestion here: How to start a new thread when old one finishes? is to use ThreadPool, I tried as follows: def test_post(): print "Executing in " + threading.currentThread().getName() + "\n" time.sleep(randint(1,

Exchange data between two Python processes

有些话、适合烂在心里 提交于 2020-01-15 06:29:30
问题 I have an Arduino which sends a JSON packet to a Python process (PP1). This Python process will run continuously. But this process has to invite and receive JSON packets to another Python process (PP2). Basically PP1 has to pass the JSON packet received from Arduino to PP2. And PP1 has to receive commands packets from PP2 (can be in JSON format too). Link to architecture image: Bellow a begin the code of Python process 1 import json #open port serialport = serial.Serial('COM5',38400,timeout=1

How to control a Python GUI via HTTP API

天大地大妈咪最大 提交于 2020-01-14 06:19:07
问题 I want a Python program that implements an HTTP API (e.g. using Flask) on which it can receive messages to show various windows on the screen (e.g. using tkinter). What is a good way of structuring such a program? I believe I will need two separate threads: one for drawing the tkinter windows and one for listening for HTTP requests. say, I want to send an http request to e.g. /show_window, then a window is shown and kept on screen until a request is sent to /hide_window, and the window is

Looping in the GUI

假如想象 提交于 2020-01-07 06:39:26
问题 I have a panel in wxPython where I want to take a button enable monitor and when clicked start a loop of the below, but also free up the GUI again and thusly update the button label to disable monitor . Once disable is clicked, it would stop the loop entirely. I've looked at threading , but I am not sure that is what I should be doing in this case? The entire loop runs within a def startStop(self) declaration and is run within the wxPanel's class . I'm in way over my head, but I've been

Updating variable values when running a thread using QThread in PyQt4

女生的网名这么多〃 提交于 2020-01-06 07:23:09
问题 So problem occurred when I tried using Threading in my code. What I want to do is passing default values to the def __init__ and then calling the thread with its instance with updated values but somehow I cannot get the updated values. Below is my initial code: main.py from PyQt4 import QtGui import sys import GUI # GUI app by using PYQT4 from PyQt4.QtCore import QThread #import Photos class PyMain(QtGui.QWidget, GUI.Ui_Pycloud): def __init__(self): super(self.__class__, self).__init__() self

Python threading or multiprocessing for web-crawler?

一曲冷凌霜 提交于 2020-01-06 07:16:47
问题 I've made simple web-crawler with Python. So far everything it does it creates set of urls that should be visited, set of urls that was already visited. While parsing page it adds all the links on that page to the should be visited set and page url to the already visited set and so on while length of should_be_visited is > 0. So far it does everything in one thread. Now I want to add parallelism to this application, so I need to have same kind of set of links and few threads / processes,

pyside widget run with threading.Thread class

醉酒当歌 提交于 2020-01-06 07:07:27
问题 I have obtained a widget from a QtDesigner and converted .ui file to .py file by pyside. now I want that widget to organize a database and an apart threading.Thread (in same module) to open and read database and send to UDP. the fact that I know how to deal with all these apartly but when bringing together it is hard. should I use thread as a class inside my widget class which is: def setupUi(self, Form): ... def retranslateUi(self, Form): ... if __name__ == "__main__": ... Form.show() and

Values are not inserted into MySQL table using pool.apply_async in python2.7

假如想象 提交于 2020-01-06 06:51:16
问题 I am trying to run the following code to populate a table in parallel for a certain application. First the following function is defined which is supposed to connect to my db and execute the sql command with the values given (to insert into table). def dbWriter(sql, rows) : # load cnf file MYSQL_CNF = os.path.abspath('.') + '/mysql.cnf' conn = MySQLdb.connect(db='dedupe', charset='utf8', read_default_file = MYSQL_CNF) cursor = conn.cursor() cursor.executemany(sql, rows) conn.commit() cursor

tkinter error on python 3 (RuntimeError: Calling Tcl from different appartment)

戏子无情 提交于 2020-01-05 08:53:14
问题 The below code doesn't work on python3.5 (RuntimeError: Calling Tcl from different appartment) But It works well on python 2.7 It is hard to know the reason of problem and how can i fix it. import tkinter import threading class MyTkApp(threading.Thread): def __init__(self): self.root=tkinter.Tk() self.s = tkinter.StringVar() self.s.set('Foo') l = tkinter.Label(self.root,textvariable=self.s) l.pack() threading.Thread.__init__(self) def run(self): self.root.mainloop() app = MyTkApp() app.start(