0

I'm importing multiple python threads from different directories and then want to run them simultaneously.

Here's my parent:

import sys
import thread

sys.path.append('/python/loanrates/test')

import test2

thread.start_new_thread(test2.main())

and here's one of my child's:

import json 

def main():

    data = 'ello world'

    print data 

    with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
        json.dump(data, f)

if __name__ == '__main__':
    main() 

but I am getting this error:

TypeError: start_new_thread expected at least 2 arguments, got 1

What is a simple way I can get this thread started (and then sequentially run multiple threads using the same method)

Idos
  • 15,053
  • 14
  • 60
  • 75
David Hancock
  • 1,063
  • 4
  • 16
  • 28

1 Answers1

1

You also need to provide a tuple with the argument to run the function with. If you have none, pass an empty tuple.

thread.start_new_thread(test2.main, ())

From the docs of thread.start_new_thread(function, args[, kwargs]) (boldface mine):

Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

You can also:

thread = Thread(target = test2.main, args, kwargs)

thread.start() // starts the thread

thread.join() // wait

Read more on this approach to creating and working with threads here.

Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75
  • I get invalid syntax from using this: `thread.start_new_thread(test2.main, ( , ) )` – David Hancock Feb 21 '16 at 11:35
  • Also using `thread = Thread(target = test2.main, args, kwargs)` i get Thread is not defined – David Hancock Feb 21 '16 at 11:36
  • "Thread is not defined" -> import the library (http://stackoverflow.com/questions/28873479/python-nameerror-global-name-thread-is-not-defined). I am checking regarding the syntax error now – Idos Feb 21 '16 at 11:37
  • Check it now (removed the comma `,` ) – Idos Feb 21 '16 at 11:39
  • Thanks, however Is still get this error `Unhandled exception in thread started by sys.excepthook is missing lost sys.stderr` – David Hancock Feb 21 '16 at 11:40
  • The exception happens when the main thread (the one that starts other threads) finishes. In your code the main thread quits before any of your sub threads (created by start_new_thread) finish. The solution is to wait at your main thread till the child threads ends. This answer demonstrates how to use threads well: http://stackoverflow.com/a/9532410/2204926 – Idos Feb 21 '16 at 11:41
  • Hm sorry I don't understand, how do i 'wait' ? – David Hancock Feb 21 '16 at 11:44
  • Using `join` :) (http://stackoverflow.com/questions/25967093/how-to-wait-for-a-spawned-thread-to-finish-in-python) – Idos Feb 21 '16 at 11:48