Can threads create sub-threads in Python?

Viewed 12975

I am familiar with the syntax for creating threads in python.

from threading import Thread
from queue import Queue

task_queue = Queue(maxsize=0)    

num_threads=10
for i in range(num_threads):
    thread = Thread(target=work, args=(task_queue,))
    thread.start()

task_queue.join()

My question is weather it is ok to open up new threads 'inside' other threads like so:

def work(task_queue):
    task = task_queue.get()

    subtasks = task.get_sub_tasks()

    for subtask in subtasks:
        thread = Thread(target=sub_work, args(subtask,))
        thread.start()

So

  1. Is this structure ok? or is it to messy to do it this way?

  2. If this is ok, are the sub-thread processes subordinated to the thread that generated it, or do they become children of the parent python process? If the thread that created the sub-thread "dies" with an error, what happens to the sub-thread?

I realize python threads are subject to the interpreter global lock, but my application involves access to a server, so the multi threading is to avoid serialized connections which would take too long.

1 Answers
Related