我无法将协同程序链接在一起.在一个比hello world或factorial稍微不那么简单的例子中,我想要一个循环来持续监视文件修改时间,然后在触摸文件时打印出时间:
#!/usr/bin/env python3
import os
import asyncio
@asyncio.coroutine
def pathmonitor(path):
modtime = os.path.getmtime(path)
while True:
new_time = os.path.getmtime(path)
if new_time != modtime:
modtime = new_time
yield modtime
yield from asyncio.sleep(1)
@asyncio.coroutine
def printer():
while True:
modtime = yield from pathmonitor('/home/users/gnr/tempfile')
print(modtime)
loop = asyncio.get_event_loop()
loop.run_until_complete(printer())
loop.run_forever()
Run Code Online (Sandbox Code Playgroud)
我希望这可以工作 - 但是,当我运行它时,我得到一个:
RuntimeError: Task got bad yield: 1426449327.2590399
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
更新:请参阅下面的答案,了解观察者模式的示例(即,在文件被触摸时有效地允许多个注册者获得更新),而不使用回调(您必须使用任务).
UPDATE2:有一个更好的解决方案:3.5 async for
(异步迭代器):https://www.python.org/dev/peps/pep-0492/
我有一个相当复杂的GUI,通过tkinter
在Linux 上运行的python编写,其中一个组件(具有经常更新的Text小部件)导致GUI不经常崩溃(每天一次).
guis正在通过X11和Gnome 2.28.2在Mac OSX上运行,并且具有相同的行为.我的python版本是3.3,tk/tcl版本是8.5.我得到的错误是:
X Error of failed request: BadIDChoice (invalid resource ID chosen for this connection)
Major opcode of failed request: 148 (RENDER)
Minor opcode of failed request: 4 (RenderCreatePicture)
Resource id in failed request: 0x116517f
Serial number of failed request: 15106831
Current serial number in output stream: 15106872
Run Code Online (Sandbox Code Playgroud)
一个strace
样子:
11:03:29.632041 recvfrom(13, 0x3bae1d4, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
11:03:29.632059 recvfrom(13, 0x3bae1d4, 4096, 0, 0, 0) = -1 EAGAIN (Resource …
Run Code Online (Sandbox Code Playgroud) 我在python(3)中使用tk,虽然我认为这适用于任何语言.我希望得到标题栏之外的tk窗口的当前x,y坐标:
import tkinter
root = tkinter.Tk()
Run Code Online (Sandbox Code Playgroud)
但是,使用root.winfo_y()
给我的坐标包括标题栏的深度.对于屏幕左上角的窗口:
root.winfo_x(), root.winfo_y() # returns (0, 22)
Run Code Online (Sandbox Code Playgroud)
换句话说,运行:
root.geometry('+{}+{}'.format(root.winfo_x(), root.winfo_y()))
Run Code Online (Sandbox Code Playgroud)
每当我调用它时,它将向下移动22个像素(标题栏的高度).如何获得整个窗口的实际坐标?
我对这种行为感到有点困惑(使用python 3.2):
class Bar:
pass
bar = Bar()
bar.__cache = None
print(vars(bar)) # {'__cache': None}
class Foo:
def __init__(self):
self.__cache = None
foo = Foo()
print(vars(foo)) # {'_Foo__cache': None}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于双下划线如何导致属性名称被"损坏"的内容,但在上述两种情况下我都希望使用相同的名称.
有什么想法在这里发生了什么?
python attributes double-underscore private-methods python-3.x
我正在寻找一种干净,简单的方法来更新继承自基类的类级字典.例如:
class Foo(object):
adict = {'a' : 1}
class Bar(Foo):
adict.update({'b' : 2}) # this errors out since it can't find adict
Run Code Online (Sandbox Code Playgroud)
以便:
Foo.adict == {'a' : 1}
Bar.adict == {'a' : 1, 'b' : 2}
Run Code Online (Sandbox Code Playgroud)
我不想在这里使用实例,如果可能的话也不使用类方法.
想知道是否有人对使用Python的全局与引用模块本身有任何想法.虽然过去我在需要时使用全局,但我发现第二种方法更加清晰(最近倾向于支持这种语法):
import sys
mod = sys.modules[__name__]
counter = 0
def incrementGlobal():
global counter
counter += 1
def incrementMod():
mod.counter += 1
Run Code Online (Sandbox Code Playgroud)
显然他们两个都很好,但如果有人有任何强烈的意见(什么是pythonic,表演等),我很乐意听到他们.
顺便说一句,在模块自然地封装了单个类的所有方法和属性,而不是引用incrementmodule.IncrementClass.counter的情况下,我最终会使用其中任何一个,我只能使用incrementmodule.counter.
我一直在尝试在运行Lion的mac上设置ipython 3(运行2.7正常),但似乎无法摆脱以下错误:
192:~ mlauria$ /Library/Frameworks/Python.framework/Versions/3.2/bin/ipython3
/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages\
/IPython/utils/rlineimpl.py:96: RuntimeWarning: Leopard libedit detected - \
readline will not be well behaved including some crashes on tab completion, and \
incorrect history navigation. It is highly recommended that you install readline,\
which is easy_installable with: 'easy_install readline'
RuntimeWarning)
Python 3.2.2 (v3.2.2:137e45f15c0b, Sep 3 2011, 17:28:59)
Run Code Online (Sandbox Code Playgroud)
easy_install readline在ipython 2.7上正确地解决了这个问题,但是这并没有修复3.2.
我正在尝试动态添加类属性,但不是在实例级别。例如我可以手动执行以下操作:
class Foo(object):
a = 1
b = 2
c = 3
Run Code Online (Sandbox Code Playgroud)
我希望能够做到:
class Foo(object):
dct = {'a' : 1, 'b' : 2, 'c' : 3}
for key, val in dct.items():
<update the Foo namespace here>
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点,而无需从类外部调用该类(因此它是可移植的),或者不需要额外的类/装饰器。这可能吗?
有没有办法保留腌制对象的身份,即具有以下打印True
:
import pickle
class Foo:
pass
x = Foo()
print(x is pickle.loads(pickle.dumps(x))) #False
Run Code Online (Sandbox Code Playgroud)
我在Linux机器上使用cPickle和cpython 3.x,不需要可移植的东西.
有没有一种方法可以使用python“触摸” Linux中的现有目录,以使其modtime成为当前系统时间?
在命令行中,这等效于touch $directory
。
这是一个让我发疯的问题,我想不出解决办法。我的程序:
#!/usr/bin/sh
ssh -t myuser@anotherhost "cat ~/severalLineFile"
Run Code Online (Sandbox Code Playgroud)
和~/severalLineFile
上anotherhost
看起来像:
line1
line2
line3
line4
line5
line6
Run Code Online (Sandbox Code Playgroud)
当我自己运行我的程序时,终端的输出看起来像预期的那样。但是,当调整我的终端大小使其只有 5 行,并将我的程序通过管道减少时,它看起来像:
line1
line2
line3
line4
:
Run Code Online (Sandbox Code Playgroud)
并且在那时按下空格键时,它会打印出line5
和line6
(以及任何其他行),例如:
line5
line6
Run Code Online (Sandbox Code Playgroud)
现在我明白这是在伪终端中运行 ssh 的结果,并且这种阶梯式的发生是因为换行符中包含回车符。我试过使用,stty ocrnl
但这并没有达到我想要的效果,即在我按下空格键后, less 的初始打印表现得像所有东西。
顺便说一句,我需要ssh
在-t
模式下运行,因为我希望所有ctrl+C
键盘中断都能进入远程进程。如果在这方面有解决方案,我全神贯注。我在 Linux Server 6.1 上,终端是通过 Mac OS 10.6.8
我还尝试\r\n
将伪终端生成的 替换为\n
,但这并不能解决问题。
python-3.x ×9
python ×8
linux ×3
attributes ×2
tk-toolkit ×2
tkinter ×2
class ×1
dictionary ×1
easy-install ×1
global ×1
identity ×1
inheritance ×1
ipython ×1
module ×1
pickle ×1
pty ×1
readline ×1
shell ×1
ssh ×1
unix ×1
variables ×1
x11 ×1