0

Currently I'm trying to call a function with a different function that loops the initial function until a keystroke from myself. Here's the code I'm trying to run:

def input_thread(a_list):
    raw_input()
    a_list.append(True)

def loop_display(function):
    a_list = []
    thread.start_new_thread(input_thread, (a_list,))
    while not a_list:

        function
        #print "next"

        time.sleep(2)

loop_display(function)

The problem is that it only runs the called function once.

When I print something in the loop, it loops that print call, but the passed in function only once.

Ideally, I'd like to put this loop into another file and call it when needed. But that won't work if I can't call another function through it, correct?

I pulled the loop code from this answer.

Note: If it matters, the keystroke ending the function works fine.

FallenSpaces
  • 312
  • 1
  • 4
  • 17
  • why don't you use normal synchronization primitives, like locks and semaphores? – Marat Apr 15 '18 at 02:21
  • @Marat I don't know what any of those things are. I'm self taught so there are some things that aren't so obvious to me yet. – FallenSpaces Apr 15 '18 at 02:23
  • I guess something like [Event](https://docs.python.org/2/library/threading.html#event-objects) can simplify the synchronization part a bit. Also, there are non-blocking ways to check stdin, which will eliminate the need in threads at all – Marat Apr 15 '18 at 02:32
  • @Marat do you know how that code would look? This seems like one of those simple things that needs a less than simple piece of code. – FallenSpaces Apr 15 '18 at 02:39

1 Answers1

1

I think you just need to add brackets after function.

`

def function():
    print("hello")

import thread
import time
def input_thread(a_list):
    raw_input()
    a_list.append(True)

def loop_display(function):
    a_list = []
    thread.start_new_thread(input_thread, (a_list,))
    while not a_list:

        function()
        #print "next"

        time.sleep(1)

loop_display(function)

`

Ryan Lim
  • 144
  • 6