python中多线程编程中EOFError报错问题

fxmzssy 2018-03-14 05:01:53
最近学习python多进程编程时,发现一个问题,不知道是什么原因,请大家帮忙看看
python版本为2.7
我写了一个基于进程池的多进程处理消费者生产者模式的Python代码,如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from multiprocessing import Process, JoinableQueue, Pool
import time
import os

import MySQLdb

os.environ["PYTHONOPTIMIZE"] = '1'


def proxy(cls_instance):
return cls_instance.process()


class ProdConsumProc(object):
def __init__(self, queue,name):
self.queue = queue
self.name = name

def producter(self):
print "[%s]dealhour producter:begin...." % str(self.name)
for row in range(10):
self.queue.put(row)
time.sleep(1)
self.queue.join()

def consumer(self):
print "[%s]dealhour consumer:begin...." % str(self.name)
while True:
item = self.queue.get()
print "[%s]consumer process username:%s" % (str(self.name),item)
time.sleep(1)
self.queue.task_done()


class BatchProc(object):
def __init__(self, conscount,name):
self.cusumnum = conscount
self.name = name

def process(self):
taskqueue = JoinableQueue()
for i in range(self.cusumnum):
print "%s start consumer:begin...." % str(self.name)
consumerservice = ProdConsumProc(taskqueue, self.name)
consumer_p = Process(target=consumerservice.consumer, args=())
consumer_p.daemon = True
consumer_p.start()

print "%s start producter:begin...." % str(self.name)
productservice = ProdConsumProc(taskqueue, self.name)
productservice.producter()


if __name__ == '__main__':
pool = Pool(processes=3)
for num in range(2):
_kls = BatchProc(5, num)
# pool.apply_async(self.func, args=(num,))
pool.apply_async(proxy, args=(_kls, ))
time.sleep(10)
pool.close()
pool.join()



这段代码运行一切,当我想给类ProdConsumProc初始化函数中增加一个数据库连接时(增加代码第20行),却出现了问题,代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from multiprocessing import Process, JoinableQueue, Pool
import time
import os

import MySQLdb

os.environ["PYTHONOPTIMIZE"] = '1'


def proxy(cls_instance):
return cls_instance.process()


class ProdConsumProc(object):
def __init__(self, queue,name):
self.queue = queue
self.name = name
self.conn = MySQLdb.connect(host="173.23.2.16", user="user", passwd="1qsx", db="icp3.0", charset="utf8")

def producter(self):
print "[%s]dealhour producter:begin...." % str(self.name)
for row in range(10):
self.queue.put(row)
time.sleep(1)
self.queue.join()

def consumer(self):
print "[%s]dealhour consumer:begin...." % str(self.name)
while True:
item = self.queue.get()
print "[%s]consumer process username:%s" % (str(self.name),item)
time.sleep(1)
self.queue.task_done()


class BatchProc(object):
def __init__(self, conscount,name):
self.cusumnum = conscount
self.name = name

def process(self):
taskqueue = JoinableQueue()
for i in range(self.cusumnum):
print "%s start consumer:begin...." % str(self.name)
consumerservice = ProdConsumProc(taskqueue, self.name)
consumer_p = Process(target=consumerservice.consumer, args=())
consumer_p.daemon = True
consumer_p.start()

print "%s start producter:begin...." % str(self.name)
productservice = ProdConsumProc(taskqueue, self.name)
productservice.producter()


if __name__ == '__main__':
pool = Pool(processes=3)
for num in range(2):
_kls = BatchProc(5, num)
# pool.apply_async(self.func, args=(num,))
pool.apply_async(proxy, args=(_kls, ))
time.sleep(10)
pool.close()
pool.join()


错误日志如下:
0 start consumer:begin....
1 start consumer:begin....
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\Python27\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "D:\Python27\lib\pickle.py", line 1384, in load
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "D:\Python27\lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "D:\Python27\lib\pickle.py", line 1384, in load
return Unpickler(file).load()
File "D:\Python27\lib\pickle.py", line 864, in load
dispatch[key](self)
File "D:\Python27\lib\pickle.py", line 886, in load_eof
return Unpickler(file).load()
File "D:\Python27\lib\pickle.py", line 864, in load
raise EOFError
EOFError
dispatch[key](self)
File "D:\Python27\lib\pickle.py", line 886, in load_eof
raise EOFError
EOFError

Process finished with exit code 0

后来我试着把这个数据库连接放在类的其他方法中(增加代码第23行和31行),程序又运行正常了,代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from multiprocessing import Process, JoinableQueue, Pool
import time
import os

import MySQLdb

os.environ["PYTHONOPTIMIZE"] = '1'


def proxy(cls_instance):
return cls_instance.process()


class ProdConsumProc(object):
def __init__(self, queue,name):
self.queue = queue
self.name = name
#self.conn = MySQLdb.connect(host="173.23.2.16", user="user", passwd="1qsx", db="icp3.0", charset="utf8")

def producter(self):
self.conn = MySQLdb.connect(host="173.23.2.16", user="user", passwd="1qsx", db="icp3.0", charset="utf8")
print "[%s]dealhour producter:begin...." % str(self.name)
for row in range(10):
self.queue.put(row)
time.sleep(1)
self.queue.join()

def consumer(self):
self.conn = MySQLdb.connect(host="173.23.2.16", user="user", passwd="1qsx", db="icp3.0", charset="utf8")
print "[%s]dealhour consumer:begin...." % str(self.name)
while True:
item = self.queue.get()
print "[%s]consumer process username:%s" % (str(self.name),item)
time.sleep(1)
self.queue.task_done()


class BatchProc(object):
def __init__(self, conscount,name):
self.cusumnum = conscount
self.name = name

def process(self):
taskqueue = JoinableQueue()
for i in range(self.cusumnum):
print "%s start consumer:begin...." % str(self.name)
consumerservice = ProdConsumProc(taskqueue, self.name)
consumer_p = Process(target=consumerservice.consumer, args=())
consumer_p.daemon = True
consumer_p.start()

print "%s start producter:begin...." % str(self.name)
productservice = ProdConsumProc(taskqueue, self.name)
productservice.producter()


if __name__ == '__main__':
pool = Pool(processes=3)
for num in range(2):
_kls = BatchProc(5, num)
# pool.apply_async(self.func, args=(num,))
pool.apply_async(proxy, args=(_kls, ))
time.sleep(10)
pool.close()
pool.join()


请各位大神看看是什么原因?多谢
...全文
2283 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
带佳前行 2020-03-05
  • 打赏
  • 举报
回复
引用 3 楼 青青啊 的回复:
网上好多回答是把线程设置为0 但是会降低运行速度 怎么破
最后破了吗?
青青啊 2019-12-18
  • 打赏
  • 举报
回复
网上好多回答是把线程设置为0 但是会降低运行速度 怎么破
fxmzssy 2018-03-16
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry 的回复:
eof 错误。文件加载有问题
为何放在初始化方法里不行,但是放到其他方法里就不报错了呢
oyljerry 2018-03-14
  • 打赏
  • 举报
回复
eof 错误。文件加载有问题

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧