python-multithreading

Why does my Python thread with subprocess not work as expected?

僤鯓⒐⒋嵵緔 提交于 2019-12-11 06:42:16
问题 I have the following subtle problem: The Python code starts two threads, each creating a subprocess call to an exexutable (in fact written in C). The first executable is passed the argument 10000, meaning a delay of 10s before its exits. Similar for the second executable, but with a delay of 20s. I observe, that worker1 and worker 2 do their printouts at the same time, namely after 20s, the longer of both delays. Why is wait() for worker1 somehow "blocked" by the longer delay of the other?

Python returning values from infinite loop thread

风格不统一 提交于 2019-12-11 06:00:23
问题 So for my program I need to check a client on my local network, which has a Flask server running. This Flask server is returning a number that is able to change. Now to retrieve that value, I use the requests library and BeautifulSoup. I want to use the retrieved value in another part of my script (while continuously checking the other client). For this I thought I could use the threading module. The problem is, however, that the thread only returns it's values when it's done with the loop,

Is there a way to stop timer creep?

妖精的绣舞 提交于 2019-12-11 05:27:52
问题 I am looking for a way to run a method every second, regardless of how long it takes to run. In looking for help with that, I ran across Run certain code every n seconds and in trying it, found that it doesn't work correctly. It appears to have the very problem I'm trying to avoid: drift. I tried adding a "sleep(0.5)" after the print, and it does in fact slow down the loop, and the interval stays at the 1.003 (roughly) seconds. Is there a way to fix this, to do what I want? (venv) 20170728

How NOT to wait for a thread to finish in Python

别来无恙 提交于 2019-12-11 05:14:18
问题 In this question, he actually asked something like what I want. Except that the answer was to remove the parentheses. However if I remove the parentheses, then I'll not be able to pass arguments for my functions. How can I do the following simple code without waiting: from time import sleep import threading def whatever(i): sleep(5) print("Hey! It's me number " + str(i)) for i in range(3): t = threading.Thread(target=whatever(i)) t.start() Desired output would be Hey! It's me number 0 Hey! It

Python , Timeout on a function on child thread without using signal and thread.join

心已入冬 提交于 2019-12-11 05:03:29
问题 I want to add a timeout on one function which is getting called inside a child thread. I can't use a signal, as a signal should be on the main thread. I can't use thread.join(time_out) , as that function can sometimes be executed in a few seconds, and in those cases the thread will always wait out the time_out . Are there any other approaches? Sources: thread.join : Timeout function using threading in python does not work signal : Timeout function if it takes too long to finish 来源: https:/

Returning data to the original process that invoke a subprocess

不想你离开。 提交于 2019-12-11 04:20:03
问题 Someone told me to post this as a new question. This is a follow up to Instantiating a new WX Python GUI from spawn thread I implemented the following code to a script that gets called from a spawned thread (Thread2) # Function that gets invoked by Thread #2 def scriptFunction(): # Code to instantiate GUI2; GUI2 contains wx.TextCtrl fields and a 'Done' button p = subprocess.Popen("python secondGui.py", bufsize=2048, shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE) # Wait for a

How do I use threads on a generator (multiple threads per item) while keeping the order?

南楼画角 提交于 2019-12-11 04:17:41
问题 I have a code that is mimicking a REST API call (see below). For every key in the item of the generator, it needs to run a REST call. So in my example, a record could be {"a": 2, "b": 36, "c": 77} I need to run a REST call for every key ( a , b , and c ) individually, then output the results (which just negates the number): {"a": 2, "a_neg": -2, "b": 36, "b_neg": -36, "c": 77, "c_neg": -77} Right now my current code works for one key, but with multiple keys, it will repeat the items (so I'm

Amazon AWS - python for beginners

点点圈 提交于 2019-12-11 00:28:30
问题 I have a computationally intensive program doing calculations that I intend to parallelise. It is written in python and I hope to use the multiprocess module. I would like some help with understanding what I would need to do to have one program run from my laptop controlling the entire process. I have two options in terms of what computers I can use. One is computers which I can access through ssh user@comp1.com from the terminal ( not sure how to access them through python ) and then run the

Creating widgets that automatically update in gtk while still being able to control other widgets

孤街浪徒 提交于 2019-12-10 22:59:10
问题 Ok, In one of my side projects in my process to learn more Python I have been trying to build a gtk app to monitor water temp and update a text box within a gtk app at 10 second intervals. I also want to be able to have a countdown timer that can be displayed in gtk while refreshing every second. I have a GUI built by using glade and gtk but I ran into the issue of the app locking up and becoming unresponsive. After a bit of reading I have figured out that I am going to have to use threading.

What am I missing in python-multiprocessing/multithreading?

╄→гoц情女王★ 提交于 2019-12-10 21:48:16
问题 I am creating, multiplying and then summing all elements of two big matrices in numpy. I do this some hundred times with two methods, a loop and with the help of the multiprocessing modul (see the snipet below). def worker_loop(n): for i in n: mul = np.sum(np.random.normal(size=[i,i])*np.random.normal(size=[i,i])) def worker(i): mul = np.sum(np.random.normal(size=[i,i])*np.random.normal(size=[i,i])) n = range(100,300) pool = ThreadPool(2) pool.map(worker, n) pool.close() pool.join() worker