Dims
Dims

Reputation: 51009

How to pass parameters to the thread, operating on tensorflow graph?

I would like to run a thread, which will populate a queue on the computation graph from normal python operations. The thread will use sess object, some of tensors and some data.

How to pass data to the thread?

Here is seen some sample: https://stackoverflow.com/a/34596212/258483

load_and_enqueue function should be ran in a separate thread. It is started with

t = threading.Thread(target=load_and_enqueue)
t.start()

but how sess and enqueue_op reach inside of a thread? These declared as function parameters, so it is not a closure. So, is this author's error or Python allows this way of sending parameters?

Upvotes: 0

Views: 464

Answers (1)

mrry
mrry

Reputation: 126154

If you have a function load_and_enqueue(foo, bar, baz) and you want to run it in a thread with particular values for the arguments, you can start it with those arguments as follows:

def load_and_enqueue(foo, bar, baz):
  # ...

foo_arg = ...
bar_arg = ...
baz_arg = ...

t = threading.Thread(target=load_and_enqueue, args=(foo_arg, bar_arg, baz_arg))
t.start()

N.B. The original SO answer has been modified because it was intended to simply capture the variables from the enclosing scope.

Upvotes: 3

Related Questions