Python-同时运行两个命令

Python-同时运行两个命令,第1张

Python-同时运行两个命令

这可以通过在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()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5648795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存