Python - run two commands simultaneously

I am new to Python and am having problems with this piece of code:

while true: rand = random.choice(number) print(rand) enter_word = input("Write something: ") time.sleep(5) 

I want to be able to enter words in the console, and at the same time , random numbers appear in the console. But the new number appears only after entering the word. What is the best way to get both of these commands working at the same time?

Do I need to create a stream or is there something simpler that I can do? And if I need to make a thread, can you please work a little bit on how I will create it?

Thanks in advance

0
source share
4 answers

This can be achieved using the multiprocessing module in python, please find the code below

 #!/usr/bin/python from multiprocessing import Process,Queue import random import time def printrand(): #Checks whether Queue is empty and runs while q.empty(): rand = random.choice(range(1,100)) time.sleep(1) print rand if __name__ == "__main__": #Queue is a data structure used to communicate between process q = Queue() #creating the process p = Process(target=printrand) #starting the process p.start() while True: ip = raw_input("Write something: ") #if user enters stop the while loop breaks if ip=="stop": #Populating the queue so that printramd can read and quit the loop q.put(ip) break #Block the calling thread until the process whose join() #method is called terminates or until the optional timeout occurs. p.join() 
+1
source

To wait for input and display some random output at the same time, you can use a graphical interface ( something with an event loop ):

screenshot

 #!/usr/bin/env python3 import random from tkinter import E, END, N, S, scrolledtext, Tk, ttk, W class App: password = "123456" # the most common password def __init__(self, master): self.master = master self.master.title('To stop, type: ' + self.password) # content frame (padding, etc) frame = ttk.Frame(master, padding="3 3 3 3") frame.grid(column=0, row=0, sticky=(N, W, E, S)) # an area where random messages to appear self.textarea = scrolledtext.ScrolledText(frame) # an area where the password to be typed textfield = ttk.Entry(frame) # put one on top of the other self.textarea.grid(row=0) textfield.grid(row=1, sticky=(E, W)) textfield.bind('<KeyRelease>', self.check_password) textfield.focus() # put cursor into the entry self.update_textarea() def update_textarea(self): # insert random Unicode codepoint in U+0000-U+FFFF range character = chr(random.choice(range(0xffff))) self.textarea.configure(state='normal') # enable insert self.textarea.insert(END, character) self.textarea.configure(state='disabled') # disable editing self.master.after(10, self.update_textarea) # in 10 milliseconds def check_password(self, event): if self.password in event.widget.get(): self.master.destroy() # exit GUI App(Tk()).master.mainloop() 
0
source

I want to be able to type words in the console, while random numbers appear in the console.

 #!/usr/bin/env python import random def print_random(n=10): print(random.randrange(n)) # print random number in the range(0, n) stop = call_repeatedly(1, print_random) # print random number every second while True: word = raw_input("Write something: ") # ask for input until "quit" if word == "quit": stop() # stop printing random numbers break # quit 

where call_repeatedly() is defined here .

call_repeatedly() uses a separate thread to call print_random() again.

0
source

you need to start two simultaneous threads at the same time to get rid of such a lock. it looks like there are two interpreters that run your code, and each of them executes a specific section of your project.

0
source

Source: https://habr.com/ru/post/1202873/


All Articles