Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AsyncResult.successful()返回false,get()引发属性错误_Python_Multithreading_Python 2.7_Threadpool_Python Multithreading - Fatal编程技术网

Python AsyncResult.successful()返回false,get()引发属性错误

Python AsyncResult.successful()返回false,get()引发属性错误,python,multithreading,python-2.7,threadpool,python-multithreading,Python,Multithreading,Python 2.7,Threadpool,Python Multithreading,我第一次尝试在多处理模块中使用Python的ThreadPool来加速一些非常慢的日志解析 不幸的是,它似乎不能正常工作。我也无法通过谷歌搜索找到任何有类似案例的人。我调用pool.join(),等待线程完成,然后对其进行迭代以访问其返回值。但是,我发现AsyncResult.ready()返回true,而AsyncResult.successful()返回false。当我调用get()时,会引发一个属性错误 Traceback (most recent call last): File "

我第一次尝试在多处理模块中使用Python的ThreadPool来加速一些非常慢的日志解析

不幸的是,它似乎不能正常工作。我也无法通过谷歌搜索找到任何有类似案例的人。我调用
pool.join()
,等待线程完成,然后对其进行迭代以访问其返回值。但是,我发现AsyncResult.ready()返回true,而AsyncResult.successful()返回false。当我调用get()时,会引发一个属性错误

Traceback (most recent call last):
  File "C:\Users\luke.timothy\Documents\Aptana Studio 3 Workspace\Monitor\monitor.py", line 175, in <module>
    stamp = threads[i].get()
  File "C:\Python27\lib\multiprocessing\pool.py", line 528, in get
    raise self._value
AttributeError: _strptime

有人能帮忙吗?我以前从未使用过这个模块,就我的谷歌搜索结果而言,学习这个模块的材料非常稀少。官方的Python文档只能让我了解到目前为止……

你遇到了datetime库中的线程安全问题,根据

上周五,我遇到了一个Python Bug,所以这个周末我花了一些时间 调查这个错误并写这篇文章来解释根本原因。 我不是Python专家,而是C程序员。如果你发现 有什么错误请纠正我

我在这里提取了最小化POC:

#/usr/bin/env python
导入线程
导入时间
def螺纹_fn():
对于x范围(1,10)内的uu:
对于X范围内的ux(1100):
时间.strtime(“2013-06-02”,%Y-%m-%d”)
对于X范围(10)内的uu:
线程。启动新线程(线程fn,())
时间。睡眠(1)
上层代码有时会抛出异常:
AttributeError:
_strtime_time
,您可以在您的环境中运行它并检查输出

我检查了Python-2.7.2(Mac默认)和Python-2.7.3(从 源代码)。我随机得到这个错误,这意味着有时候 剧本很好用

以及解决办法:

您应该意识到这将是一个多线程问题,对吗?这是 时间的实现

静态PyObject*
时间\u strtime(PyObject*self,PyObject*args)
{
PyObject*strtime\u module=PyImport\u ImportModuleNoBlock(“\u strtime”);
PyObject*strtime\u结果;
如果(!strtime_模块)
返回NULL;
strtime_result=PyObject_CallMethod(strtime_模块,
“_strptime_time”,“O”,args);
Py_DECREF(strtime_模块);
返回strtime_结果;
}
每次调用此函数时,它都会尝试加载模块 “_strptime”。API PyImport\u ImportModuleNoBlock的算法是 如果有线程正在导入该模块,它将抛出 异常,而不是在那里阻塞。这避免了重复模块 导入和潜在的死锁

但在多线程环境中,当一个线程试图导入 _strtime,但尚未完全导入,另一个线程试图直接调用
strtime\u模块。这就是为什么这个错误
发生了

如果您很了解此错误发生的原因,您应该已经知道了 你心中的解决方法。实际上这很简单。 您只需在开始您的应用程序之前调用一次
strtime
线程。

因此,您似乎可以通过在创建线程之前直接导入
\u strtime
来解决这个问题

,其中有更多信息

def parse(all_logs):
    current_time_local = datetime.now()
    print "Parsing."
    stamps = []
    for line in all_logs:
        match = re.match(match_string, line)
        if match:
            for i in range(4):
                if match.group(1 + (i * 3)):
                    wheren = match.group(1 + (i * 3)).rstrip().strip("[").strip("]").split(",")
                    break

            stamp = datetime.strptime(wheren[0], "%Y-%m-%d %H:%M:%S")
            if stamp.day == current_time_local.day or (stamp.day  == current_time_local.day-1 and stamp.hour >= current_time_local.hour):
                try:
                    name, aliases, ipaddrlist = socket.gethostbyaddr(wheren[1].split(":")[1])
                except:
                    continue
                stamps.append(Event(stamp,name,match.groups()))
    print "Returning stamps."
    return stamps

pool = ThreadPool(processes=8)

threads = []

for i in range(8):
    range_begin = i * logs_fraction
    range_end = range_begin + logs_fraction
    print "begin: " + str(range_begin) + " end: " + str(range_end)
    thread_args = []
    thread_args.extend(all_logs[range_begin:range_end])
    threads.append(pool.apply_async(parse, (thread_args, )))

pool.close()

pool.join()

for i in range(8):
    print "Getting thread " + str(i+1)
    print threads[i].ready()
    print threads[i].successful()
    print "Thread Ready."
    stamp = threads[i].get()
    print stamp
    stamps.extend(stamp)