Python 为什么Pymongo要关闭连接中间进程?

Python 为什么Pymongo要关闭连接中间进程?,python,mongodb,pymongo,Python,Mongodb,Pymongo,我正在Jupyter笔记本中运行一个python脚本,该脚本连接到mongo db以处理文本文档(新闻文章)。对于前几批数据,脚本运行正常,但随后终止,并出现以下错误: AutoReconnect:localhost:27017:[WinError 10054]远程主机强制关闭了现有连接(下面是完整错误) 已经尝试按照此处()的规定删除mongod.lock文件,但是,这并没有解决问题 这是我的函数(这里也不包括一些子函数): 完全错误: ~\Anaconda3\envs\py35\lib\si

我正在Jupyter笔记本中运行一个python脚本,该脚本连接到mongo db以处理文本文档(新闻文章)。对于前几批数据,脚本运行正常,但随后终止,并出现以下错误:

AutoReconnect:localhost:27017:[WinError 10054]远程主机强制关闭了现有连接(下面是完整错误)

已经尝试按照此处()的规定删除
mongod.lock
文件,但是,这并没有解决问题

这是我的函数(这里也不包括一些子函数):

完全错误:

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
    578                            use_op_msg=self.op_msg_enabled,
--> 579                            unacknowledged=unacknowledged)
    580         except OperationFailure:

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, session, client, check, allowable_errors, address, check_keys, listeners, max_bson_size, read_concern, parse_write_concern_error, collation, compression_ctx, use_op_msg, unacknowledged)
    140         else:
--> 141             reply = receive_message(sock, request_id)
    142             unpacked_docs = reply.unpack_response(codec_options=codec_options)

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in receive_message(sock, request_id, max_message_size)
    172     length, _, response_to, op_code = _UNPACK_HEADER(
--> 173         _receive_data_on_socket(sock, 16))
    174     # No request_id for exhaust cursor "getMore".

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in _receive_data_on_socket(sock, length)
    231             try:
--> 232                 chunk_length = sock.recv_into(mv[bytes_read:])
    233             except (IOError, OSError) as exc:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

AutoReconnect                             Traceback (most recent call last)
<ipython-input-14-37a7effc9859> in <module>
----> 1 collect_df_by_sources(collections_dir, sources, newspapers_df, filter_year='2008')

<ipython-input-8-bbd9e966e95c> in collect_df_by_sources(collections_dir, sources, newspapers_df, return_df, filter_year, min_year, override)
     20 
     21         print("collect articles from: {}, {}".format(source_name, source_id))
---> 22         source_df = collect_df_by_source(collections_dir, source_id, filter_year=filter_year, min_year=min_year)
     23         source_df.to_csv(source_path, encoding='utf8')
     24         if return_df:

<ipython-input-7-82d129ff329c> in collect_df_by_source(mongo_collections_dir, source, clean_text, filter_year, min_year)
      9             print('filter year not found') # - note added by dror for debug 24/10/2019
     10             filter_dict["extracted.publication_date.year"] = {"$gt": min_year}
---> 11         source_count = mongo_collection.count(filter_dict)
     12         print("{} articles found in collection {} {}".format(source_count, collection, filter_year))
     13         if source_count == "0":

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in count(self, filter, session, **kwargs)
   1764         collation = validate_collation_or_none(kwargs.pop('collation', None))
   1765         cmd.update(kwargs)
-> 1766         return self._count(cmd, collation, session)
   1767 
   1768     def create_indexes(self, indexes, session=None, **kwargs):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in _count(self, cmd, collation, session)
   1570                 read_concern=self.read_concern,
   1571                 collation=collation,
-> 1572                 session=session)
   1573         if res.get("errmsg", "") == "ns missing":
   1574             return 0

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in _command(self, sock_info, command, slave_ok, read_preference, codec_options, check, allowable_errors, read_concern, write_concern, collation, session, retryable_write)
    242                 session=s,
    243                 client=self.__database.client,
--> 244                 retryable_write=retryable_write)
    245 
    246     def __create(self, options, collation, session):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
    582         # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
    583         except BaseException as error:
--> 584             self._raise_connection_failure(error)
    585 
    586     def send_message(self, message, max_doc_size):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in _raise_connection_failure(self, error)
    741         self.close()
    742         if isinstance(error, socket.error):
--> 743             _raise_connection_failure(self.address, error)
    744         else:
    745             raise error

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in _raise_connection_failure(address, error, msg_prefix)
    281         raise NetworkTimeout(msg)
    282     else:
--> 283         raise AutoReconnect(msg)
    284 
    285 

AutoReconnect: localhost:27017: [WinError 10054] An existing connection was forcibly closed by the remote host```


~\Anaconda3\envs\py35\lib\site packages\pymongo\pool.py in命令(self、dbname、spec、slave\u ok、read\u首选项、codec\u选项、check、allowed\u errors、check\u key、read\u concern、write\u concern、parse\u write\u concern\u error、collation、session、client、retryable\u write、publish\u事件)
578使用\u op\u msg=self.op\u msg\u启用,
-->579未确认=未确认)
580除操作故障外:
~\Anaconda3\envs\py35\lib\site packages\pymongo\network.py in命令(sock、dbname、spec、slave\u ok、is\u mongos、read\u preference、codec\u选项、会话、客户端、检查、允许的错误、地址、检查键、侦听器、最大检查大小、读关注点、解析关注点、排序、压缩\u ctx、使用\u op\u msg、未确认)
140其他:
-->141回复=接收消息(sock、请求id)
142解包文档=回复。解包响应(编解码器选项=编解码器选项)
接收消息中的~\Anaconda3\envs\py35\lib\site packages\pymongo\network.py(sock、请求\u id、最大消息\u大小)
172长度、响应、操作码=\u解包\u头(
-->173_在_插座上接收_数据(插座,16))
174#没有排气光标“getMore”的请求id。
~\Anaconda3\envs\py35\lib\site packages\pymongo\network.py in\u-receive\u-data\u-on\u-socket(sock,长度)
231试试:
-->232 chunk\u length=sock.recv\u into(mv[字节读取:)
233除(IOError、OSError)外,作为exc:
ConnectionResetError:[WinError 10054]远程主机已强制关闭现有连接
在处理上述异常期间,发生了另一个异常:
自动重新连接回溯(最近一次呼叫最后一次)
在里面
---->1按来源收集数据(收集目录、来源、报纸数据、过滤数据(2008年))
在“按来源收集”中(收集目录、来源、报纸、返回、筛选年、最小年、覆盖)
20
21打印(“从:{},{}收集文章”。格式(来源名称,来源id))
--->22 source\u df=按源收集\u df\u(收集\u dir,source\u id,filter\u year=filter\u year,min\u year=min\u year)
23源文件到csv(源文件路径,编码='utf8')
24如果返回_df:
按来源收集数据(mongo\u collections\u dir、source、clean\u text、filter\u year、min\u year)
9打印(“未找到筛选年份”)#-dror为2019年10月24日的调试添加的注释
10筛选器目录[“摘录.发布日期.年份”]={“$gt”:最小年份}
--->11 source\u count=mongo\u collection.count(filter\u dict)
12打印(“{}在集合{}{}中找到的{}篇文章”。格式(源计数、集合、筛选年))
13如果源_计数=“0”:
计数中的~\Anaconda3\envs\py35\lib\site packages\pymongo\collection.py(self、filter、session、**kwargs)
1764 collation=validate_collation_或_none(kwargs.pop('collation',none))
1765命令更新(kwargs)
->1766返回自计数(cmd、排序规则、会话)
1767
1768 def create_索引(self,index,session=None,**kwargs):
~\Anaconda3\envs\py35\lib\site packages\pymongo\collection.py in\u count(self、cmd、collation、session)
1570阅读关注=自我阅读关注,
1571排序规则=排序规则,
->1572会话=会话)
1573如果res.get(“errmsg”,则“)==”缺少ns”:
1574返回0
~\Anaconda3\envs\py35\lib\site packages\pymongo\collection.py in\u命令(self、sock\u info、command、slave\u ok、read\u首选项、codec\u选项、检查、允许的\u错误、read\u关注点、write\u关注点、排序规则、会话、可重试的\u write)
242会话=s,
243 client=self.\u database.client,
-->244可重试写入=可重试写入)
245
246定义创建(自我、选项、排序规则、会话):
~\Anaconda3\envs\py35\lib\site packages\pymongo\pool.py in命令(self、dbname、spec、slave\u ok、read\u首选项、codec\u选项、check、allowed\u errors、check\u key、read\u concern、write\u concern、parse\u write\u concern\u error、collation、session、client、retryable\u write、publish\u事件)
582#捕捉套接字错误、键盘中断等并关闭我们自己。
583除BaseException作为错误外:
-->584自我提升连接故障(错误)
585
586 def发送消息(自身、消息、最大文档大小):
~\Anaconda3\envs\py35\lib\site packages\pymongo\pool.py in\u raise\u connection\u失败(self,error)
741 self.close()
742如果isinstance(错误,套接字错误):
-->743上升连接失败(self.address,错误)
744其他:
745上升误差
~\Anaconda3\envs\py35\lib\site packages\pymongo\pool.py in\u raise\u connection\u失败(地址、错误、消息前缀)
281提升网络超时(msg)
282其他:
-->283升起自动重新连接(msg)
284
285
AutoReconnect:localhost:27017:[WinError 10054]远程主机已强制关闭现有连接```

寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题的问题告诉我
~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
    578                            use_op_msg=self.op_msg_enabled,
--> 579                            unacknowledged=unacknowledged)
    580         except OperationFailure:

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, session, client, check, allowable_errors, address, check_keys, listeners, max_bson_size, read_concern, parse_write_concern_error, collation, compression_ctx, use_op_msg, unacknowledged)
    140         else:
--> 141             reply = receive_message(sock, request_id)
    142             unpacked_docs = reply.unpack_response(codec_options=codec_options)

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in receive_message(sock, request_id, max_message_size)
    172     length, _, response_to, op_code = _UNPACK_HEADER(
--> 173         _receive_data_on_socket(sock, 16))
    174     # No request_id for exhaust cursor "getMore".

~\Anaconda3\envs\py35\lib\site-packages\pymongo\network.py in _receive_data_on_socket(sock, length)
    231             try:
--> 232                 chunk_length = sock.recv_into(mv[bytes_read:])
    233             except (IOError, OSError) as exc:

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

AutoReconnect                             Traceback (most recent call last)
<ipython-input-14-37a7effc9859> in <module>
----> 1 collect_df_by_sources(collections_dir, sources, newspapers_df, filter_year='2008')

<ipython-input-8-bbd9e966e95c> in collect_df_by_sources(collections_dir, sources, newspapers_df, return_df, filter_year, min_year, override)
     20 
     21         print("collect articles from: {}, {}".format(source_name, source_id))
---> 22         source_df = collect_df_by_source(collections_dir, source_id, filter_year=filter_year, min_year=min_year)
     23         source_df.to_csv(source_path, encoding='utf8')
     24         if return_df:

<ipython-input-7-82d129ff329c> in collect_df_by_source(mongo_collections_dir, source, clean_text, filter_year, min_year)
      9             print('filter year not found') # - note added by dror for debug 24/10/2019
     10             filter_dict["extracted.publication_date.year"] = {"$gt": min_year}
---> 11         source_count = mongo_collection.count(filter_dict)
     12         print("{} articles found in collection {} {}".format(source_count, collection, filter_year))
     13         if source_count == "0":

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in count(self, filter, session, **kwargs)
   1764         collation = validate_collation_or_none(kwargs.pop('collation', None))
   1765         cmd.update(kwargs)
-> 1766         return self._count(cmd, collation, session)
   1767 
   1768     def create_indexes(self, indexes, session=None, **kwargs):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in _count(self, cmd, collation, session)
   1570                 read_concern=self.read_concern,
   1571                 collation=collation,
-> 1572                 session=session)
   1573         if res.get("errmsg", "") == "ns missing":
   1574             return 0

~\Anaconda3\envs\py35\lib\site-packages\pymongo\collection.py in _command(self, sock_info, command, slave_ok, read_preference, codec_options, check, allowable_errors, read_concern, write_concern, collation, session, retryable_write)
    242                 session=s,
    243                 client=self.__database.client,
--> 244                 retryable_write=retryable_write)
    245 
    246     def __create(self, options, collation, session):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
    582         # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
    583         except BaseException as error:
--> 584             self._raise_connection_failure(error)
    585 
    586     def send_message(self, message, max_doc_size):

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in _raise_connection_failure(self, error)
    741         self.close()
    742         if isinstance(error, socket.error):
--> 743             _raise_connection_failure(self.address, error)
    744         else:
    745             raise error

~\Anaconda3\envs\py35\lib\site-packages\pymongo\pool.py in _raise_connection_failure(address, error, msg_prefix)
    281         raise NetworkTimeout(msg)
    282     else:
--> 283         raise AutoReconnect(msg)
    284 
    285 

AutoReconnect: localhost:27017: [WinError 10054] An existing connection was forcibly closed by the remote host```