用户输入时如何防止print()抬高输入文本

时间:2018-12-22 21:14:51

标签: python command-line-interface

基本上,我有一个异步工作人员在做事情,但是我可以随时捕获用户命令行。

问题只是视觉上的,当用户键入内容并且代码打印出一些东西时,用户键入的文本与打印的文本一起被抬起。如何使控制台的最后一行“隔离”?

这是一个示例代码:

import queue
import  threading
import time
import heapq

def worker():
    while True:
        item = q.get()
        if item is None:
            break
        do_work(item)
        #q.task_done()
        time.sleep(2)
        q.put(item)



def do_work(item):
    print(item,end = '')
    print(time.time(), end = '')


q = queue.PriorityQueue()
num_worker_threads = 1
threads = []
for i in range(num_worker_threads):
    t = threading.Thread(target=worker)
    t.start()
    threads.append(t)


fruits = [(1,"apple"), (2,"banana"), (3,"cherry")]
for x in fruits:
  q.put(x)

gameFinished = 0
# block until all tasks are done
#q.join()

while not gameFinished:
    q.put((-1,input()))

# stop workers
for i in range(num_worker_threads):
    q.put(None)
for t in threads:
    t.join()


input("Press enter to exit ;)")

0 个答案:

没有答案