1

This is similar to this here.

I am trying to do other actions during an input for a chat system i'm working on using sockets, but the method in the link doesnt seem work in python 3, with this slightly modified code:

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')

And output:

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

I have no knowledge about threads, but would like to have some useful foresight on threads, if anything.

EDIT

changed code:

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')

output:

AttributeError: module 'threading' has no attribute 'start_new_thread'
1
  • 1
    Use module threading instead (needs different way to start thread) and, by the way, add awesomespecialinput to global declaration in your getinput() Commented Jan 9, 2018 at 1:37

1 Answer 1

1

In Python 3 you can use threading.Thread with your getinput function as target parameter:

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')

(The start_new_thread method you're trying to use is not available in Python 3's threading module as that's a higher-level wrapper around the _thread API.)

Sign up to request clarification or add additional context in comments.

5 Comments

when I run this, it prints waiting.what are you thinking about What I expect for it to print is: waiting. waiting.. what are you thinking about -- waiting. waiting.. waiting... what are you thinking about -- waiting. waiting.. waiting... waiting.... what are you thinking about -- waiting. waiting.. waiting... waiting.... waiting..... what are you thinking about is this possible? sorry if i'm asking for too much
@crazicrafter1 Do you mean it prints "waiting" before it asks for input?
I didn't realize the last comment I made would be so dense, the expected output is supposed to be this: imgur.com/a/GtzOZ
'waiting.what are you thinking about'
@crazicrafter1 Are you sure you're not running the thread multiple times?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.