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

就可以正常退出了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值