python 使用多进程无法正常退出

https://segmentfault.com/q/1010000006686575

# !/usr/bin/python
# -*- coding: utf-8 -*-

import multiprocessing
import random
import time


class UseQueue(object):
	def __init__(self):
		self.init_value = ""
		self.new_value = ""
		self.queue = None

	def update_data(self, cur_str, queue):
		self.queue = queue
		if queue.qsize():
			self.init_value = queue.get()

		new_value = self.init_value + cur_str

		self.new_value = new_value
		# 保存数据到queue
		self.__save_data_queue()

	# 保存数据到queue
	def __save_data_queue(self):
		self.queue.put(self.new_value)
		self.__write_file()

	# 写入文件
	def __write_file(self):
		file_path = "./a_test.txt"
		with open(file_path, "w") as file_handle:
			file_handle.write(self.new_value)


def call_func(str1, queue):
	# 处理逻辑耗时
	time.sleep(2)
	queue_class = UseQueue()
	queue_class.update_data(str1, queue)
	print("call_func:str1.len %r" % len(str1))


# 测试进程间的队列通讯
def __process_queue():
	# manager = multiprocessing.Manager()
	# queue = manager.Queue()
	queue = multiprocessing.Queue()

	pro_list = []
	arg_list = []
	# 列表个数
	for i in range(4):
		total_str = "%s======" % i
		# 每个字符串长度
		for j in range(1000):
			random_str = random.choice(["aaaaaaaaaaaaaaaaaaaaaaaaaaa\n", "bbbbbbbbbbbbbbbbbbbbbbbb\n", "cccccccccccccccccc\n"])
			total_str += "%s" % random_str

		total_str += "-----------------------------------------------------------\n"
		arg_list.append(total_str)

	for arg in arg_list:
		one_pre = multiprocessing.Process(target=call_func, args=(arg, queue))
		pro_list.append(one_pre)
		one_pre.start()

	for one_pre in pro_list:
		one_pre.join()
		print("process end")


if __name__ == '__main__':
	__process_queue()

这时候只有3个 -- “process end”,4个--“call_func:str1.len”,最后的进程无法退出

这时候只要

queue = multiprocessing.Queue()

改成

manager = multiprocessing.Manager()

queue = manager.Queue()

就可以正常退出了。

你可能感兴趣的:(Python)