这可以通过在python中使用多处理模块来实现,请找到以下代码
#!/usr/bin/pythonfrom multiprocessing import Process,Queueimport randomimport timedef printrand(): #Checks whether Queue is empty and runs while q.empty(): rand = random.choice(range(1,100)) time.sleep(1) print randif __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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)