python-multithreading

Running a timer for few minutes in python

浪子不回头ぞ 提交于 2019-12-24 17:04:01
问题 I am trying to run a certain function "foo" every second. I have to do this for a few minutes (say 5). The function foo() makes 100 HTTP Requests (which contains a JSON object) to the server and prints the JSON response. In short, I have to make 100 HTTP requests per second for 5 minutes. I have just started learning python, thus don't have extensive knowledge. This is what I have tried: import threading noOfSecondsPassed = 0 def foo(): global noOfSecondsPassed # piece of code which makes 100

python locking and threading concurrency [duplicate]

柔情痞子 提交于 2019-12-24 11:47:50
问题 This question already has answers here : Threading Best Practices (11 answers) Closed 6 years ago . i have a question regards python locks and threading, i realise locks are used to prevent variables from being overwritten by another thread, is it normal to use locks to get around this issue as it then means you can only run a single thread concurrently, it also means creating acquire/release locks for every variable that maybe overwritten, which for my project runs into quite a few! how are

Timeout on a Python function call inside timeit

被刻印的时光 ゝ 提交于 2019-12-24 10:46:30
问题 I have a function, let's call it my_function(*args, **kwargs) , that depending on the arguments passed to it takes anywhere from 30 seconds to many many hours (days). I have a list of different arguments and I want to know how long the functions will take given the arguments. I am still very new to using timeit , but I have learn enough to do this part; however, this wouldn't be the very efficient for my purposes. Any set of arguments that take longer than 4 hours to complete are consider

Python threading issue in OpenCV

我们两清 提交于 2019-12-24 08:07:05
问题 I was using threading module with OpenCV, I came accross a bizarre issue. When using thread I was unable to start the camera again to take video input. I would take one frame and then stop. While there was no problem like this when using multiprocessing module. I am unable to understand what causes this strange behaviour. This code summarizes the problem I have, The program will stuck when the thread is created second time. import cv2 import time import threading def open_cam(): count = 0 cam

Why is my thread executing the target function before I start it?

妖精的绣舞 提交于 2019-12-24 06:38:08
问题 I would like to understand why I must wait for my receiver thread to end its work before I can do anything else. I understand that my sock_listen function is awaiting a connection, that's what its meant for, but I don't understand why this isn't happening "within" my thread. Sorry if this is a stupid question but I'm kind of lost ! Thank you in advance! def sock_listen(address, port): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (address,port) print("Starting

How can I efficiently implement multithreading/multiprocessing in a Python web bot?

我只是一个虾纸丫 提交于 2019-12-24 04:32:19
问题 Let's say I have a web bot written in python that sends data via POST request to a web site. The data is pulled from a text file line by line and passed into an array. Currently, I'm testing each element in the array through a simple for-loop. How can I effectively implement multi-threading to iterate through the data quicker. Let's say the text file is fairly large. Would attaching a thread to each request be smart? What do you think the best approach to this would be? with open("c:\file.txt

Producer Consumer using semaphores and mutexes in Python

社会主义新天地 提交于 2019-12-24 03:52:40
问题 I'm trying to understand how to implement a Queue with a bounded buffer size that can be used by multiple producers and consumers using Python Semaphores. Here's my implementation: class Q: def __init__(self, size): self.buff = [None]*size self.end = 0 self.start = 0 self.size = size self.end_lock = Lock() # protect end from race across multiple producers self.start_lock = Lock() # protect start from race across multiple consumers self.open = Semaphore(size) # block till there's space to

How to pass arguments to win32com event handler

自闭症网瘾萝莉.ら 提交于 2019-12-24 03:41:34
问题 The code below works fine. I can't find a way to pass some arguments to EventHandler or to call methods of MainClass from EventHandler . For example instead of using constant param , I'd like to pass it through constructor or setter method. I've tried recommendations from here. But in this case EventHandler instance does not catch any events (or at least nothing appears in stdout). class EventHandler: param = "value" def OnConnected(self): print 'connected' return True class MainClass: def

value based thread lock

十年热恋 提交于 2019-12-23 12:52:45
问题 Forgive me if this has been asked before. I have looked around a lot, but I get the feeling that I don't have the proper vocabulary to find this by searching the web. I have a multithreaded application in python. I want to be able to lock a certain block of code but only to other threads with a certain condition. Let me give an example: There are three threads, thread_a , thread_b and thread_c . Each thread may run through the function foo at any time. I don't want any two threads with bar

Django global data for threads

走远了吗. 提交于 2019-12-23 12:48:27
问题 I have a shared global data object in my single-process multi-threaded django server - an object which is frequently used, but calculated infrequently. The calculation is time-consuming, so I want to share the results. I thought it would work to use django's LocalMemCache for this simple data. Oddly, it seems to work for multiple ajax calls on a single page load, but for some reason, when I reload the page in my browser, the cache is empty again. What am I doing wrong? Is there a better way?