线程可以在Python中创建子线程吗?

时间:2017-07-04 14:33:58

标签: python multithreading

我熟悉在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()

我的问题是天气可以打开新线程'内部'其他线程如此:

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()

所以

  1. 这个结构好吗?或者这样做是不是很麻烦?

  2. 如果可以的话,子线程是否从属于生成它的线程,或者它们是否是父python进程的子进程?如果创建子线程的线程"死掉"如果出现错误,子线程会发生什么?

  3. 我意识到python线程受解释器全局锁定的影响,但我的应用程序涉及访问服务器,因此多线程是为了避免序列化连接需要太长时间。

1 个答案:

答案 0 :(得分:8)

关于你的问题:

  • Q1:启动" subthreads"不是问题。来自一个帖子
  • Q2:这实际上是一个有趣的问题,我的直觉会说" no",但获得一个证据对我来说听起来更好

所以我创建了一个快速测试,如下所示(我会使用一个要点但我无法从我所在的地方访问这些东西):

from threading import Thread
import time

def sub_worker(id):
    print("SubWorker started from thread", id)
    while True:
        print("Subworking...")
        time.sleep(5)
def worker(id):
    print("Worker started from thread", id)
    count = 1
    while count < 5:
        print("Working...")
        tmp_thread = Thread(target=sub_worker, args=[count])
        tmp_thread.start()
        count +=1
        time.sleep(1)
    raise EnvironmentError("Tired of working")

main = Thread(target=worker, args=[0])

main.start()

这给了我们输出(正如预期的那样,父线程中的错误不会阻止&#34;孩子&#34;):

Worker started from thread 0
Working...
SubWorker started from thread 1
Subworking...
Working...
SubWorker started from thread 2
Subworking...
Working...
SubWorker started from thread 3
Subworking...
Working...
SubWorker started from thread 4
Subworking...
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Temp\tt\Tools\Anaconda3.4.3.1\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Temp\tt\Tools\Anaconda3.4.3.1\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Temp/tt/Tools/PyCharmWorkspace/xml_parse/test.py", line 18, in worker
    raise EnvironmentError("Tired of working")
OSError: Tired of working

Subworking...
Subworking...
Subworking...
Subworking...
Subworking...
Subworking...
Subworking...

我认为htop显示这种层次结构可能是由于Linux内核threads are treated as processes的事实。因为调用fork就可以显示这个层次结构。有了线程的概念,我不相信层次结构如此有意义,因为它们中的每一个都将共享相同的资源(内存,文件描述符......等)