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'
crazicrafter1
  • 309
  • 5
  • 18
  • 1
    Use module `threading` instead (needs different way to start thread) and, by the way, add `awesomespecialinput` to `global` declaration in your `getinput()` – Michael Butscher Jan 09 '18 at 01:37

1 Answers1

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

Arminius
  • 2,363
  • 1
  • 18
  • 22
  • 1
    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 Jan 09 '18 at 01:49
  • @crazicrafter1 Do you mean it prints "waiting" before it asks for input? – Arminius Jan 09 '18 at 01:57
  • I didn't realize the last comment I made would be so dense, the expected output is supposed to be this: https://imgur.com/a/GtzOZ – crazicrafter1 Jan 09 '18 at 01:57
  • 'waiting.what are you thinking about' – crazicrafter1 Jan 09 '18 at 01:59
  • @crazicrafter1 Are you sure you're not running the thread multiple times? – Arminius Jan 09 '18 at 02:32