I have 10 threads, the problem is when something wrong happens raising the exception, bye will be printed 10 times.
I just want to print it once, then terminate all the threads. Is there any solution for the problem?
from threading import Thread
def printmsg(msg,threadNumber):
while True:
try:
print 'this is your message %s -- Thread Number:%s'%(msg,threadNumber)
except:
exit('Bye')
for i in range(0,11):
Thread(target=printmsg,args=('Hello Wrold',str(i))).start()
You could set a flag in the threads. n the main loop, you could join() all threads successively in order to wait until they are gone and then print a message if the flag is set.
The flag could even be the value of the exception...
from threading import Thread, Lock
stop = False
lock = Lock()
def printmsg(msg, threadNumber):
global stop
while True:
try:
if threadNumber in [3, 5, 7, 9]: # Something wrong happens
raise NotImplementedError
lock.acquire()
if stop:
lock.release()
break
print 'This is your message %s -- Thread Number: %s' % (msg, threadNumber)
lock.release()
except NotImplementedError:
lock.acquire()
if not stop:
stop = True
print 'Bye'
lock.release()
break
for i in range(0,11):
Thread(target=printmsg, args=('Hello World', i)).start()
Try to join all the child threads by the main thread. And do your job in main thread.
#-*-coding:utf-8-*-
from threading import Thread
def printmsg(msg,threadNumber):
while True:
try:
print 'this is your message %s -- Thread Number:%s'%(msg,threadNumber)
raise
except:
break
if __name__ == '__main__':
threads = []
for i in range(0,11):
threads.append(Thread(target=printmsg,args=('Hello Wrold',str(i))))
for t in threads:
t.start()
for t in threads:
t.join()
exit('Bye')
Related
i have a thread running on the background checking every 5 sec and print a msg after 5 secs passes.
in loop1 the msg should appear at specific point. (in this case, its above print('test')).
the thread can wait longer than 5 secs when loop1 is running (like a few sec delay is fine) but it have not to appear at the same time as loop1 is running so i put queue to for blocking.
when loop2 is running, the messsage in the multithread should appear while loop2 is running at the same time unlike loop no1.
so i didnt put queue.get in loop2 since theres no need for blocking but the problem is when going into loop3. if we go into loop3 while we are on time.sleep(5) in multithread, thats fine but if we are on print ("thread working2") in multithread, i want to make sure it waits until it does all the work in print ("thread working2") part. there are more codes there but i just put a simple print for better readability. before going into loop3. is there any way i can accomplish this? and is the method im using right now is suitable for my intention?
sorry for my bad english!
import threading
import time
from queue import Queue
queue = Queue()
queue2 = Queue()
switch = False
def func0():
while True:
global switch
global queue
global queue2
time.sleep(5)
switch = True
print ("switch on")
a = queue.get()
if a == 1:
print("thread working")
time.sleep(0.5)
print("thread working")
time.sleep(0.5)
switch = False
queue2.put(True)
if a == 2:
print("thread working2")
time.sleep(0.5)
print("thread working2")
time.sleep(0.5)
switch = False
if __name__ == "__main__":
t1 = threading.Thread(target=func0)
t1.start()
testnumber = 0
testnumber2 = 0
testnumber3 = 0
while True: #loop no.1
if switch:
queue.put(1)
queue2.get()
print ("loop1")
time.sleep(0.5)
testnumber = testnumber +1
if testnumber == 200:
break
while True: #loop no.2
if switch:
queue.put(2)
print ("loop2")
time.sleep(0.5)
testnumber2 = testnumber2 + 1
if testnumber2 == 200:
break
while True: #loop no.3
print ("loop3")
time.sleep(0.5)
testnumber3 = testnumber3 + 1
if testnumber3 == 200:
break
t1.join()
I have made a thread that is supposed to show the seconds passing. Unfortunately, when I use getstr from the curses module the whole script stops, including the thread. I have to use a thread lock to stop random characters being printed out because of overlapping orders.
Any suggestions on how to fix this or an alternative would be great!
In the below example window and window2 are already set-up...
lock = threaing.Lock()
def main_function():
#starts thread
t1 = threading.Thread(target=time_calc,args=(window2,))
t1.start()
#Gets user input
while True:
data = window1.addstr(y,x,"Type something in!")
data = window1.getstr(y,x,5)
lock.acquire()
window1.erase()
txt = "You said: "+data
window1.addstr(y,x,txt)
lock.release()
def time_calc(window2):
current_count = 0
while True:
time += 1
text = "Time Count: "+str(time)
lock.acquire()
window2.erase()
window2.addstr(y,x,text)
lock.release()
time.sleep(1)
The problem with my code
I figured out the problem with my code. You can't run a thread inside a thread for some reason and I originally had my main function called to be considered a thread. I guess I should have stated this in my question. Sorry
There is probably a way to run a thread in a thread, but this did not work for me.
My Updated Code
lock = threading.Lock()
def call_threads():
t1 = threading.Thread(target=main_function,args=(window1,))
t1.start()
t2 = threading.Thread(target=time_calc,args=(window2,))
t1.start()
def main_function(window1):
#Gets user input
while True:
data = window1.addstr(y,x,"Type something in!")
data = window1.getstr(y,x,5)
lock.acquire()
window1.erase()
txt = "You said: "+data
window1.addstr(y,x,txt)
lock.release()
def time_calc(window2):
current_count = 0
while True:
time += 1
text = "Time Count: "+str(time)
lock.acquire()
window2.erase()
window2.addstr(y,x,text)
lock.release()
time.sleep(1)
If there is anything else that may have caused this error, please comment it!
I have some background task that i want to start an be able to safely quit by user input.
To do that i have a thread in which a process pool with a continous task is started. There is a input lock to stop printing and to wait for user input and a event to stop the whole process.
What surprises me is that the processes seem to start and do their work if there is a time.sleep after the start of the thread with processpool (6th line in main).
import multiprocessing as mp
import time
import threading as tr
def init(e, l):
global stop_event
global input_lock
stop_event = e
input_lock = l
def stupid_task(n):
while not stop_event.is_set():
with input_lock:
print(n)
time.sleep(2)
def test_mng(n, event, lock):
with mp.Pool(n, initializer=init, initargs=(event, lock,)) as p:
print("before")
p.map(stupid_task, range(1, n + 1))
print("after")
p.close()
p.join()
def main():
i_lock = mp.Lock()
s_event = mp.Event()
thread = tr.Thread(target=test_mng, args=(3, s_event, i_lock))
init(s_event,i_lock)
thread.start()
time.sleep(1) # if this line is commented out only "before" is printed
while not stop_event.is_set():
input("")
with input_lock:
print("stopped")
eingabe = input("type q to quit")
if eingabe == "q":
stop_event.set()
if __name__ == "__main__":
main()
I ask myself what is stopping the process pool from doing it's work. Do i do something fundamentally wrong? The time.sleep seems a little bit hacky.
I my opinion, you are running your script from an IDE (like PyCharm), but not from the Console. Your IDE is catching the keyboard events.
You can simplify the processing:
The main process can wait for the user input,
The thread can do the "stupid task".
Here is a possible solution:
# coding: utf-8
import multiprocessing as mp
import threading as tr
import time
stop_event = None
def init(event):
global stop_event
stop_event = event
def stupid_task(n):
while not stop_event.is_set():
print(n)
time.sleep(2)
def test_mng(n, event):
with mp.Pool(n, initializer=init, initargs=(event,)) as p:
print("before")
p.map(stupid_task, range(1, n + 1))
print("after")
p.close()
p.join()
def main():
print("type 'q' <ENTER> to quit")
s_event = mp.Event()
init(s_event)
thread = tr.Thread(target=test_mng, args=(3, s_event,))
thread.start()
while not stop_event.is_set():
c = input("")
if c in "qQ":
stop_event.set()
if __name__ == "__main__":
main()
I am working on a Python scripts that kicks off a thread with a loop and a raw_input so that user can enter commands. After this thread starts, main program starts a loop with another raw_input so that the user can enter commands.
How can this be organized so that the commands being inputted via console goes to the correct raw_input (main thread/concurrent thread)? At the moment, all inputs in the console are going to the main thread only.
Thanks
Example
import threading
def commThread():
while True:
chatAcceptance = raw_input("User")
t1 = threading.Thread(target=commThread)
t1.start()
while True:
userInput = raw_input("\nPlease insert a command:\n")
So this can be done via lock. I did a small code example that shows how to swap between one "scope" to the other using the raw_input.
import threading
lock = threading.Lock()
def inputReader(thread, prompt):
userInput = raw_input(prompt)
print thread + " " + userInput + "\n"
return userInput
def myThread1():
global lock
while True:
lock.acquire()
print "thread 1 got the lock\n"
while True:
threadInput = inputReader("thread 1", "from thread 1\n")
if threadInput == "release":
lock.release()
print "thread 1 released the lock\n"
break
def myThread2():
global lock
while True:
lock.acquire()
print "thread 2 got the lock\n"
while True:
threadInput = inputReader("thread 2", "from thread 2\n")
if threadInput == "release":
lock.release()
print "thread 2 released the lock\n"
break
t1 = threading.Thread(target=myThread1).start()
t2 = threading.Thread(target=myThread2).start()
import multiprocessing as mp
import time
def build(q):
print 'I build things'
time.sleep(10)
#return 42
q.put(42)
def run(q):
num = q.get()
print num
if num == 42:
print 'I run after build is done'
return
else:
raise Exception("I don't know build..I guess")
def get_number(q):
q.put(3)
if __name__ == '__main__':
queue = mp.Queue()
run_p = mp.Process(name='run process', target=run, args=(queue,))
build_p = mp.Process(name='build process', target=build, args=(queue,))
s3 = mp.Process(name='s3', target=get_number, args=(queue,))
build_p.start()
run_p.start()
s3.start()
print 'waiting on build'
build_p.join(1) # timeout set to 1 second
s3.join()
print 'waiting on run'
run_p.join()
queue.close()
print 'waiting on queue'
queue.join_thread()
print 'done'
My goal is to send build and run into different workers, and run has to get result back from build in order to proceed.
The above revised code based on your help will actually return exception, because s3 is returned before build has the chance.
The value in the front of the queue is now 3. How can we make sure we get the answer back from build process?
Thanks.
Your question is a little murky..the problem you are describing sounds synchronous so 3 processes are a little overkill.
Assuming you are just trying to pass values to run you could use the queue object.
import multiprocessing as mp
import time
def build(q):
print 'I build things'
time.sleep(5)
q.put(42)
return
def run(q):
while True:
num = q.get()
if num == 42:
print 'I run after build is done'
return
else:
print 'not the right number...'
def get_number():
return 41
if __name__ == '__main__':
queue = mp.Queue()
run_p = mp.Process(name='run process', target=run, args=(queue,))
build_p = mp.Process(name='build process', target=build, args=(queue,))
run_p.start()
build_p.start()
print 'waiting on build'
build_p.join()
print 'waiting on run'
run_p.join()
queue.close()
print 'waiting on queue'
queue.join_thread()
print 'done'