Python Threads collection - multithreading

I have a code something like this,
import threading
class Mythread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print('do some processing')
if __name__=='__main__':
while Ture:
val = raw_input('next thread')
t = MyThread()
t.start()
t.join()
The question is how can I carry on with main function without blocking the main because t.join() stop the main until t does not finish?

You should put code in the "code" tag or else it's not really readable.
And you just have to do something like that.
if name == 'main':
#Thread creation
allThreads = []
while True:
val = raw_input('next thread')
newThread = MyThread()
newThread.start()
allThreads.append(newThread)
#You can do something here
#Waiting for all threads to stop
for thread in allThreads:
thread.join()

Related

How to implement a "background" or concurrent thread?

Figured it out, i think? Runs as expected. I'm not able to view the original code so I wrote this new one up. Is there a better way to do this?
import time
import threading
def threadee():
f = open(r'log.txt')
for line in f:
print(line)
time.sleep(0.2)
def threader():
while True:
threadee()
def main():
thread = threading.Thread(target=threader)
thread.start()
while True:
print('main thread running')
print(threading.enumerate())
time.sleep(1)
if __name__ == '__main__':
main()

Kill all threads python

Starting my script off with:
for i in range(threads):
t = Thread(target=getSizes, args=(i,))
t.start()
Then when one of the threads is able to get the variables needed for the other functions it does:
for i in range(threads):
t = Thread(target=cart, args=(i, sizes, prod_name, product_id))
t.start()
Is there any way to till all threads started on getSizes() and then start new threads on cart()?
If your worker function does work in a loop, it can use a common resource like an Event to signal when work is complete and it should return. Here is an example
import threading
import time
import random
def getSizes(done_event):
while not done_event.is_set():
print("get size")
if random.randint(0, 20) == 10:
print("got size")
done_event.set()
do_cart()
else:
time.sleep(random.random())
print("sizes done")
def do_getSizes():
event = threading.Event()
threads = []
for i in range(5):
t = threading.Thread(target=getSizes, args=(event,))
t.start()
threads.append(t)
for t in threads:
t.join()
def cart():
print("I haz the cartz")
def do_cart():
time.sleep(1)
threads = []
for i in range(5):
t = threading.Thread(target=cart)
t.start()
threads.append(t)
for t in threads:
t.join()
do_getSizes()

threading.Timer join() quits immediately?

Since threading.Timer is a subclass of Thread, I would expect that the .join() in this script would cause the code to print "woof" once a second, continually:
import threading
def target_action(arg):
print arg
def start_timer_proc(interval, arg):
timer = threading.Timer(interval, target_action, [arg])
timer.start()
return timer
def main():
timer = start_timer_proc(1.0, "woof")
timer.join()
print("...exiting")
main()
Instead, it prints out "woof" once and then terminates (without any error message). What am I missing?
Here's what I really wanted (based loosely on https://stackoverflow.com/a/12435256/558639):
import threading
class IntervalTimer(threading.Thread):
def __init__(self, target_action, interval, args=[]):
threading.Thread.__init__(self)
self.event = threading.Event()
self.target_action = target_action
self.interval = interval
self.args = args
def start(self):
while not self.event.wait(self.interval):
self.target_action(*self.args)
def target_action(arg):
print arg
def start_timer_proc(interval, arg):
timer = IntervalTimer(target_action, interval, [arg])
timer.start()
return timer
def main():
timer = start_timer_proc(1.0, "woof")
print timer
timer.join()
print("...exiting")
main()
Note that I didn't need to change my target_action() or start_timer_proc() methods, except to instantiate an IntervalTimer rather than a Timer.

Infinite threaded function in class constructor

I have a class that runs an infinite loop using threads to populate a thread-safe queue:
from threading import Thread
from Queue import Queue
import time
class factory:
def __init__(self):
self.running = True
self.q = Queue()
t = Thread(target=self.count_indefinitely)
t.start()
time.sleep(3)
print self.q.qsize()
def count_indefinitely(self):
i = 0
while self.running:
i += 1
self.q.put(i)
if __name__ == '__main__':
f = factory()
time.sleep(2)
print 'Hello!'
f.running = False
The code reaches the part where I need to print out the size of the queue. However, I can't get it to print "hello" in the main function. How should I go about fixing this?

Close a resourceful thread gracefully

I have the following function which is called from a function of a class. I would like to call this as a non blocking thread or daemon process. And I want it to be stopped gracefully taking care of any IO locks and DB locks (which happen inside foo()).
def worker(fnames):
while True:
for f in fnames:
while(not os.path.isfile(f)):
time.sleep(SLEEP_INTERVAL)
while os.stat(f).st_size < 1000000:
time.sleep(SLEEP_INTERVAL)
file= open(f, 'rb')
#Do Something
foo()
os.remove(f)
I looked in to first answer on this link and modified the code. Do I create a driver function for the above function, run the driver function from a new thread, and pass the _stop event flag from the below thread to it? Or is there a better way?
class StoppableThread(threading.Thread):
def __init__(self, target, timeout):
super(StoppableThread, self).__init__()
self._target = target
self._timeout = timeout
self._stop = threading.Event()
def run(self):
while not self.stopped():
self._stop.wait(self._timeout) # instead of sleeping
if self.stopped():
continue
self._target()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
EDIT:
I managed to think of the following solution, but I find it very nasty. Any help is appreciated:
def run(self):
while True:
for f in fnames:
while not self._stop.isSet():
while(not os.path.isfile(f)):
self._stop.wait(SLEEP_INTERVAL)
if self._stop.isSet():
break;
while os.stat(f).st_size < 1000000:
self._stop.wait(SLEEP_INTERVAL)
if self._stop.isSet():
break;
if self._stop.isSet():
continue;
file= open(f, 'rb')
#Do Something
foo()
os.remove(f)
if self._stop.isSet():
break;
if self._stop.isSet():
break;

Resources