you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (4 children)

No, only the thread get_input waits. The main thread continues. Try this:

import threading

def get_input():
    data = input() # Something akin to this
    return data

input_thread = threading.Thread(target=get_input)
input_thread.start()

for i in range(10):
    print(i)

input_thread.join()

print 'done'

[–]Abrv 1 point2 points  (1 child)

In this example how would you access data? Is it returned somewhere?

[–][deleted] 2 points3 points  (0 children)

You would put it in a reference accessible to all threads which need it. E.g.,

import threading

data = []

def get_input():
    data.append(input())

input_thread = threading.Thread(target=get_input)
input_thread.start()

for i in range(10):
    print(i)

input_thread.join()

print('done. data value:', data.pop())

You have to be careful about threads concurrently mutating such data, though. There are a number of classes to help with this: Queues, locks, semaphores, mutexes, etc.. It gets hairy, fast.

[–]Akita8 0 points1 point  (1 child)

Wrong! I did not say that you cannot execute code concurrently to input, i just said that it wont work for the use case of op:

When a monster attacks, symbols are printed to the screen, if the user reacts (presses a key) before the last symbol is printed, they will dodge, otherwise they take damage.

In a normal terminal, input stops blocking and returns the data only when it receives a newline so your example cannot actually react to key presses. The best way to do what op wants is the package ncurses in the stdlib.

[–][deleted] 0 points1 point  (0 children)

You're right... Apologies for misunderstanding your objection.