Python “不一致”;从文件初始化失败";使用多线程

Python “不一致”;从文件初始化失败";使用多线程,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有如下代码: df = pd.read_csv(filepath, header=None, parse_dates=[[0,1]]) 这是从监视线程调用的。另一个进程每分钟写入一次文件 然后通过watchdog的“on_modified”调用上述代码并进行一些处理。 它工作了几次,大多数情况下我得到以下回溯: Exception in thread Thread-1: Traceback (most recent call last): File "E:\Portable Python

我有如下代码:

df = pd.read_csv(filepath, header=None, parse_dates=[[0,1]])
这是从监视线程调用的。另一个进程每分钟写入一次文件 然后通过watchdog的“on_modified”调用上述代码并进行一些处理。 它工作了几次,大多数情况下我得到以下回溯:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "E:\Portable Python 2.7.6.1\App\Lib\threading.py", line 810, in __bootstr
ap_inner
    self.run()
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\watchdog-0.8.1-py2.7.eg
g\watchdog\observers\api.py", line 236, in run
    self.dispatch_events(self.event_queue, self.timeout)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\watchdog-0.8.1-py2.7.eg
g\watchdog\observers\api.py", line 406, in dispatch_events
    self._dispatch_event(event, watch)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\watchdog-0.8.1-py2.7.eg
g\watchdog\observers\api.py", line 401, in _dispatch_event
    handler.dispatch(event)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\watchdog-0.8.1-py2.7.eg
g\watchdog\events.py", line 343, in dispatch
    _method_map[event_type](event)
  File "tx_stats.py", line 272, in on_modified
    dur, tail_skip_count)
  File "tx_stats.py", line 209, in read_file
    df = pd.read_csv(filepath, header=None, parse_dates=[[0,1]])
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\pandas-0.14.1-py2.7-win
32.egg\pandas\io\parsers.py", line 452, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\pandas-0.14.1-py2.7-win
32.egg\pandas\io\parsers.py", line 234, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\pandas-0.14.1-py2.7-win
32.egg\pandas\io\parsers.py", line 542, in __init__
    self._make_engine(self.engine)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\pandas-0.14.1-py2.7-win
32.egg\pandas\io\parsers.py", line 679, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "E:\Portable Python 2.7.6.1\App\lib\site-packages\pandas-0.14.1-py2.7-win
32.egg\pandas\io\parsers.py", line 1041, in __init__
    self._reader = _parser.TextReader(src, **kwds)
  File "parser.pyx", line 332, in pandas.parser.TextReader.__cinit__ (pandas\par
ser.c:3218)
  File "parser.pyx", line 560, in pandas.parser.TextReader._setup_parser_source
(pandas\parser.c:5608)
IOError: Initializing from file failed
有什么不对劲吗?最令人费解的是,它有时能工作,但不是每次都能。这可能是因为读取文件的进程与关闭文件的另一进程之间存在竞争条件吗

该文件包含一些统计信息,如:

2014.09.02,00:15,111.7,63,159,134.11261, and so on
2014.09.02,00:16,126.1,08,1235,154.11353,
2014.09.02,00:17,123.5,65,100,153.11313,
2014.09.02,01:18,114.7,59,101,152.11334,
2014.09.02,01:19,111.3,42,1229,153.11283,

这听起来是一种非常典型的种族行为。你可以:1。实施一个锁,看看它是否停止故障;2.在单线程设置中检查文件是否正确加载是的,我添加了一个锁定机制,这似乎解决了问题。谢谢