输入期间的Python操作()

2024-10-02 08:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

这类似于here。你知道吗

我正尝试在输入过程中对我正在使用套接字的聊天系统执行其他操作,但链接中的方法在python 3中似乎不起作用,下面的代码稍加修改:

import thread
import time

waiting = 'waiting'
i = 0

awesomespecialinput = None

def getinput():
    global var
    awesomespecialinput = input("what are you thinking about")

thread.start_new_thread(getinput,())


while awesomespecialinput == None:
    waiting += '.'
    print(waiting)
    i += 1
    time.sleep(1)

print('it took you',i,'seconds to answer')

和输出:

Traceback (most recent call last):
  File "/home/pi/python/inputtest2.py", line 1, in <module>
    import thread
ImportError: No module named 'thread'

我不知道线程,但想有一些有用的前瞻性线程,如果有的话。你知道吗

编辑

更改代码:

import threading
import time

waiting = 'waiting'
i = 0

awesomespecialinput = None

def getinput():
    global awesomespecialinput
    awesomespecialinput = input("what are you thinking about")

threading.start_new_thread(getinput,())


while awesomespecialinput == None:
    waiting += '.'
    print(waiting)
    i += 1
    time.sleep(1)

print('it took you',i,'seconds to answer')

输出:

AttributeError: module 'threading' has no attribute 'start_new_thread'

Tags: 代码importnoneyounewtimedefthread
1条回答
网友
1楼 · 发布于 2024-10-02 08:20:00

在python3中,可以将^{}getinput函数一起用作target参数:

import threading
import time


waiting = 'waiting'
i = 0
awesomespecialinput = None

def getinput():
    global awesomespecialinput
    awesomespecialinput = input("what are you thinking about")

threading.Thread(target=getinput).start()

while awesomespecialinput is None:
    waiting += '.'
    print(waiting)
    i += 1
    time.sleep(1)

print('it took you', i, 'seconds to answer')

(您尝试使用的start_new_thread方法在python3的threading模块中不可用,因为它是^{}API的高级包装器。)

相关问题 更多 >

    热门问题