简体   繁体   English

带有线程的随机python模块加载失败

[英]Random python module loading failures with threading

I'm trying to debug an error where python import statements randomly fail, at other times they run cleanly. 我正在尝试调试一个错误,其中python import语句随机失败,而其他时候它们运行得很干净。

This is an example of the exceptions I see. 这是我看到的异常的一个示例。 Sometimes I'll see this one, sometimes I'll see another one in a different module, though it seems to always hit in one of 4 modules. 有时我会看到这个,有时我会在另一个模块中看到另一个,尽管它似乎总是出现在4个模块之一中。

ERROR:root:/home/user/projecteat/django/contrib/auth/management/__init__.py:25:     RuntimeWarning: Parent module 'django.contrib.auth.management' not found while handling absolute import
from django.contrib.contenttypes.models import ContentType

Because of the random nature, I'm almost certain it's a threading issue, but I don't understand why I would get import errors, so I'm not sure what to look for in debugging. 由于随机性,我几乎可以肯定这是一个线程问题,但是我不明白为什么会出现导入错误,因此我不确定要在调试中寻找什么。 Can this be caused by filesystem contention if different threads are trying to load the same modules? 如果不同的线程试图加载相同的模块,这可能是由文件系统争用引起的吗?

I'm trying to get Django 1.4's LiveServerTestCase working on Google App Engine's development server. 我正在尝试让Django 1.4的LiveServerTestCase在Google App Engine的开发服务器上工作。 The main thread runs django's test framework. 主线程运行django的测试框架。 When it loads up a LiveServerTestCase based test class, it spawns a child thread which launches the App Engine dev_appserver, which is a local webserver. 加载基于LiveServerTestCase的测试类时,它会生成一个子线程,该子线程将启动App Engine dev_appserver(这是本地网络服务器)。 The main thread continues to run the test, using the Selenium driver to make HTTP requests, which are handled by dev_appserver on the child thread. 主线程使用Selenium驱动程序发出HTTP请求,继续运行测试,该请求由子线程上的dev_appserver处理。

The test framework may run a few tests in the LiveServerTestCase based class before tearing down the testcase class. 在删除测试用例类之前,测试框架可以在基于LiveServerTestCase的类中运行一些测试。 At teardown, the child thread is ended. 拆卸时,子线程结束。

It looks like the exceptions are happening in the child (HTTP server) thread, mostly between tests within a single testcase class. 看起来异常发生在子线程(HTTP服务器)中,通常在单个测试用例类中的测试之间。

The code for the App Engine LiveServerTestCase class is here: https://github.com/dragonx/djangoappengine/blob/django-1.4/test.py App Engine LiveServerTestCase类的代码在此处: https : //github.com/dragonx/djangoappengine/blob/django-1.4/test.py

It's pretty hard to provide all the debugging info required for this question. 提供此问题所需的所有调试信息非常困难。 I'm mostly looking for suggestions as to why python import statements would give RuntimeWarning errors. 我主要是在寻找有关为什么python import语句会产生RuntimeWarning错误的建议。

I have a partial answer to my own question. 我对自己的问题有部分答案。 What's going on is that I have two threads running. 这是怎么回事,我有两个线程在运行。

Thread 1 is running the main internal function inside dev_appserver (dev_appserver_main) which is handling HTTP requests. 线程1正在运行dev_appserver(dev_appserver_main)内部的主要内部函数,该函数正在处理HTTP请求。

Thread 2 is running the Selenium based testcases. 线程2正在运行基于Selenium的测试用例。 This thread will send commands to the browser to do something (which then indirectly generates an HTTP request and re-enters in thread 1). 该线程将向浏览器发送命令以执行某些操作(然后间接生成HTTP请求并重新进入线程1)。 It then either issues more requests to Selenium to check status, or makes a datastore query to check for a result. 然后,它向Selenium发出更多请求以检查状态,或进行数据存储区查询以检查结果。

I think the problem is that upon handling every HTTP request, Thread 1 (dev_appserver) changes the environment so that certain folders are not accessible (folder excluded in app.yaml, as well as the environment that is not part of appengine). 认为问题在于,在处理每个HTTP请求时,线程1(dev_appserver)会更改环境,以便某些文件夹不可访问(app.yaml中排除的文件夹以及不属于appengine的环境)。 If Thread 2 happens to run some code in this time, certain imports may fail to load if they are located in these folders. 如果线程2恰巧此时运行某些代码,则某些导入文件如果位于这些文件夹中,则可能无法加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM