I'm new in python and thread ,i start learning by create a simple code but i find some defficult this code below have some function and thread also Queue function to share data between thread this is a part of my code:
def PutChar(Ch): #ask user to put the correct char b=raw_input("Enter "+Ch+" char ") while (b != Ch): b=raw_input("Enter "+Ch+" char ") return b def sleep_time():#sleep time function n=11 time.sleep(5) for i in range(1,n): time.sleep(1) if(i==5): print"\ntime almost finish rest",str(i),"second" print"Enter f char " if(q.get(False)=='f'): print(q.get()) else: pass print"\ntime finish wait more than",str(i),"second" print"\ntime finish",i def PutFchar(in_q): #function for putting the F char k=PutChar('f') putEchar() in_q.put(k) def PutEchar():#function for putting the E char PutChar('e') print("cycle correct")
and i have this code in main() function:
q=Queue.Queue() t2=threading.Thread(target=sleep_time) t1=threading.Thread(target=PutFchar, args=(q,)) t1.start() t2.start() t1.join() t2.join() print"Program finish"
i have two questions:
First: why when i use the queue function the code block ? i mean in this code i use thread for running the PutFchar() and sleep_time() functions in parallel but when i add the queue function for share data between threads functions the PutFchar work but the sleep_time function block
i have this screenshot for more understand:
Second: i call the PutEchar() in the PutFchar() function but the console show me an error so what i can do for calling function in thread function ?
this is the also another screenshot:
Finally i search for an easy way to input some words or number without blocking the code for exemple if you put a word after 10 second of waiting the console quit and program finish but if i put the time sleep and the input function i need to wait first the time sleep or the user to put some thing in console but in my way i need the time work when the user put something in console so i use thread but i find the queue also a block function
I hope find some help and thanks :)
Edited by sekmani