Python多线程和多处理在一起

时间:2017-10-20 04:41:04

标签: python multithreading multiprocessing

是否可以从单个线程生成多个进程?或者它是一个适当的设计实施?

我的代码示例是 -

def run_all_tasks(self):
    for platform in self._platforms:
        task_thread = threading.Thread(
            target=self._run_task_list, args=(
                self._get_tasks(),platform,))
        taskset_threads.append(task_thread)

    for taskset_thread in taskset_threads:
        taskset_thread.start()

    for taskset_thread in taskset_threads:
        taskset_thread.join() 

def _run_task_list(self, tasklist, platform):
    try:
        test_case_name = task.__class__.__name__

        try:
            test_case_name = task._get_test_case_name()
        except:
            test_case_name = task.__class__.__name__
            pass

        max_runtime = task.get_max_runtime()

        manager = Manager()
        self._shared_mem = manager.dict()

        for task in tasklist:
            task_proc = Process(
                target=self.proc_setup,
                args=(task, self, self._shared_mem))


            task_proc.start()
            task_proc.join(max_runtime)

但这有效,有时会出现以下错误 -

Traceback (most recent call last):
  File "C:\wor\lib\TaskSet.py", line 430, in _run_task_list
    if "warning" in self._shared_mem:
  File "<string>", line 2, in __contains__
  File "C:\Python27\lib\multiprocessing\managers.py", line 755, in _callmethod
    self._connect()
   File "C:\Python27\lib\multiprocessing\managers.py", line 742, in _connect
    conn = self._Client(self._token.address, authkey=self._authkey)
  File "C:\Python27\lib\multiprocessing\connection.py", line 167, in Client
    c = PipeClient(address)
  File "C:\Python27\lib\multiprocessing\connection.py", line 387, in PipeClient
    win32.WaitNamedPipe(address, 1000)
WindowsError: [Error 2] The system cannot find the file specified

这也可以在linux平台上看到。

0 个答案:

没有答案