blocking read on a subprocess.PIPE in python

I read the question/answer/comments on Non-blocking read on a subprocess.PIPE in python , but I felt a bit lacking. When I implemented the solution provided, I noticed that this approach works best when the sub-process ends on it own. But if the subprocess is providing a stream of information and we are looking for a single match of output, then that approach doesn't work for my needs (spe

阻止读取python中的子进程.PIPE

我读了关于python中的subprocess.PIPE的非阻塞读取的问题/回答/评论,但我觉得有点欠缺。 当我实施所提供的解决方案时,我注意到当子流程自行结束时,这种方法效果最佳。 但是,如果子进程提供了一个信息流,而且我们正在寻找输出的单个匹配项,那么这种方法不适合我的需求(特别是对Windows而言,如果这很重要的话)。 这是我的示例:ping.py import time def main(): for x in range(100): print x

Non blocking read on os.pipe on Windows

This question - How to read from an os.pipe() without getting blocked? - shows a solution how to check if os.pipe has any data for Linux, and for this you need to put the pipe into non-blocking mode: import os, fcntl fcntl.fcntl(thePipe, fcntl.F_SETFL, os.O_NONBLOCK) On Windows we have this: ImportError: No module named fcntl But os.pipe is there: >>> os.pipe() (3, 4) So, is it po

在Windows上的os.pipe上无阻塞读取

这个问题 - 如何从os.pipe()读取而不被阻塞? - 显示了一个解决方案,如何检查os.pipe是否有Linux的任何数据,为此,您需要将管道置于非阻塞模式: import os, fcntl fcntl.fcntl(thePipe, fcntl.F_SETFL, os.O_NONBLOCK) 在Windows上,我们有这样的: ImportError: No module named fcntl 但os.pipe在那里: >>> os.pipe() (3, 4) 那么,是否有可能在Windows上执行非阻塞式读取或os.pipe的内容? 在通过Sta

Tornado Web and Threads

I am new to Tornado and Python Threads. What I would like to achieve is the following: I have a Tornado web server which takes requests from users. I want to store some of the data locally and write it periodically to a data base as bulk-inserts. import tornado.ioloop import tornado.web import threading # Keep userData locally in memory UserData = {} def background(f): """ a threadin

龙卷风网络和线程

我是Tornado和Python线程的新手。 我想达到的是以下内容:我有一个Tornado Web服务器,它接受来自用户的请求。 我想在本地存储一些数据,并将其定期写入数据库作为批量插入。 import tornado.ioloop import tornado.web import threading # Keep userData locally in memory UserData = {} def background(f): """ a threading decorator use @background above the function you want to thread (run in th

Unexpected relative import behavior in Python

I ran into a very surprising relative import behavior today (unfortantely after nearly 4 hours of pulling my hair out). I have always been under the impression that if you have "Class A" inside of a module name "module_a.py" within a package named "package" that you could equivalently use either: from package.module_a import ClassA or from module_a import ClassA

Python中意外的相对导入行为

今天我遇到了一个非常令人吃惊的相对进口行为(在将头发拉出近4个小时后不幸地)。 我一直认为,如果您在名为“包”的包中的模块名称“module_a.py”内有“A类”,您可以等效地使用: from package.module_a import ClassA 要么 from module_a import ClassA 只要您从“包装”内的模块导入。 我明白这是相对重要的。 直到今天,我还没有遇到过问题,因为我需要检查对象A的实例,并且我很惊讶地发现一个非常不寻常的行为。 考

Make undirected graph directed

I've got a undirected, complete graph and would like to convert it to a directed acyclic graph with a (unidirectional) path between each node. To start off, I want to add random edges and stop once all nodes are connected. What would be an algorithm to look at (using Python, but any language will do). So for instance this graph, does not to be connected any further: A ---- B

指示无向图

我有一个无向的完整图,并希望将其转换为每个节点之间具有(单向)路径的有向无环图。 首先,我想添加随机边,并在所有节点连接后停止。 看什么算法(使用Python,但任何语言都可以)。 因此,例如这张图,不会再有任何关联: A ---- B A ---> B / => / / v C C ,但在这种情况下,所有无向边都变成有向边 A ---- B A

Emacs Python.el, Syntax Highlighting Quirks

I'm using python.el version 0.23.1 for Emacs right now. The syntax highlighting seems to be a bit off -- any variable name containing an underscore followed by a keyword will result in the keyword being highlighted. Example, "foo_list" will result in "list" being highlighted. More for my own understanding of Emacs-Lisp than anything (it's not a big deal) how do I g

Emacs Python.el,语法高亮怪癖

我现在正在为Emacs使用python.el版本0.23.1。 语法突出显示有点偏离 - 任何包含下划线后跟关键字的变量名都会导致突出显示关键字。 例如,“foo_list”将导致“列表”被突出显示。 更多的是我自己对Emacs-Lisp的理解比任何东西(这不是什么大不了的),我该如何去解决这个问题。 这里是我认为相关代码在“python.el”第312-318行的地方。 我怀疑问题出在“符号开始”的定义上,但我不知道这个名字是指什么。 (defvar python-font-l

How to have a fixture loaded for Django tests in the test runner?

I have a large set of Django test cases, all of which subclass either TestCase or TransactionTestCase . I need to add a fixture to all test cases (to handle a new way of handling settings in the database). I could of course add the fixture to the fixtures attribute of every test case. Or I could change the test cases to subclass from a custom test case subclass that sets this fixture -- and t

如何在测试运行器中为Django测试加载fixture?

我有一大组Django测试用例,它们都是TestCase或TransactionTestCase子类。 我需要为所有测试用例添加一个灯具(以处理处理数据库中设置的新方法)。 我当然可以将灯具添加到每个测试用例的fixtures属性中。 或者,我可以将测试用例从设置此固定设备的自定义测试用例子类中更改为子类 - 然后将所有现有测试用例固定设置更改为扩展而不是覆盖父类固定设置。 在这种特殊情况下,上述任何一种都很乏味且容易出错。 那么 - 有

Can I test a Django app without having the initial

I want to test Django applications with special test data that is different from my initial_data fixtures. I added a test fixture to my test case, but Django still loads the initial_data fixtures . How can it be avoided? As you can see in this SO answer, you must choose another name for your normal fixture. initial_data.[xml/yaml/json] is always loaded automatically, even if you define anot

我可以测试一个Django应用程序,而无需初始化

我想使用与我的initial_data设备不同的特殊测试数据来测试Django应用程序。 我在我的测试用例中添加了一个测试装置,但Django仍然加载了initial_data装置 。 如何避免? 正如你可以在这个答案中看到的那样,你必须为普通夹具选择另一个名字。 initial_data.[xml/yaml/json]总是自动加载,即使您为测试定义了另一个夹具。 将您的灯具重命名为任何其他名称 每次需要数据时,在syncdb后使用loaddata

Why don't scripting languages output Unicode to the Windows console?

The Windows console has been Unicode aware for at least a decade and perhaps as far back as Windows NT. However for some reason the major cross-platform scripting languages including Perl and Python only ever output various 8-bit encodings, requiring much trouble to work around. Perl gives a "wide character in print" warning, Python gives a charmap error and quits. Why on earth after

为什么脚本语言不能将Unicode输出到Windows控制台?

Windows控制台至少有十年的时间可以识别Unicode,也许早于Windows NT。 然而,由于某些原因,包括Perl和Python在内的主要跨平台脚本语言只能输出各种8位编码,需要解决很多麻烦。 Perl给出了“宽字符打印”的警告,Python给出了charmap错误并退出。 为什么这些年来他们不只是简单地调用输出UTF-16 Unicode的Win32-W API,而不是通过ANSI /代码页瓶颈强制所有内容? 仅仅是跨平台性能低优先级? 是否这些语言在内部使用UTF-8

Refresh PyDev import paths in Eclipse

I'm using Eclipse Helios under Ubuntu. Whenever I install a new library under /usr/local/lib/python2.6/dist-packages/ using pip , Eclipse doesn't see it and complains about invalid imports. I double checked the library is in place. Importing it works from the interpreter. Refreshing the PyDev configuration or restarting the IDE doesn't work. Note: the mentioned path is on the li

在Eclipse中刷新PyDev导入路径

我在Ubuntu下使用Eclipse Helios。 每当我在/usr/local/lib/python2.6/dist-packages/下使用pip安装一个新库时,Eclipse都没有看到它,并且抱怨导入无效。 我仔细检查了图书馆是否到位。 导入它的工作来自口译员。 刷新PyDev配置或重新启动IDE不起作用。 注意:提到的路径在Python解释器选项卡的库路径列表中。 有没有人知道这一点的补救措施,如果不是如何查明实际原因。 问题是我从一个鸡蛋安装了库。 鸡蛋文件夹需