如何在循环中读取用户输入(并且不阻塞此循环中的工作)?

发布于 2024-12-29 04:44:59 字数 124 浏览 1 评论 0原文

如何在循环中读取用户输入(并且不阻塞此循环中的工作)?

我想做一些基本的事情,比如切换 DEBUG 变量、在用户将打印的某些特定键上打印某些变量的值等,但我的程序在恒定循环中工作,并且该循环触发另一个线程。我该怎么做?

How to read users input when in loop (and without blocking work in this loop)?

I want to do some basic stuff, like switching DEBUG variable, print values of some variables etc on some specific keys that user will print, but my program work in constant loop, and this loop fire another threads. How can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的心境我的脸 2025-01-05 04:44:59

使用线程:(

import threading
import time

value = 3

def process():
    while True:
        print(value)
        time.sleep(1)

thread = threading.Thread(target=process)
thread.start()

while True:
    value = input('Enter value: ')

由于两个循环都将内容打印到终端,输出在这里有点混乱,但我认为这个想法应该很清楚。)

Use threads:

import threading
import time

value = 3

def process():
    while True:
        print(value)
        time.sleep(1)

thread = threading.Thread(target=process)
thread.start()

while True:
    value = input('Enter value: ')

(Output gets kind of messed up here because of both loops printing stuff to the terminal but I think the idea should be clear.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文