小编Pet*_*tri的帖子

如何将coroutine添加到正在运行的asyncio循环?

如何为正在运行的asyncio循环添加新的协同程序?IE浏览器.一个已经执行了一套协同程序的程序.

我想作为一种解决方法,可以等待现有协程完成,然后初始化一个新循环(使用附加协程).但有更好的方法吗?

python asynchronous python-3.x python-asyncio

29
推荐指数
5
解决办法
3万
查看次数

TCP上的Python SysLogHandler:处理连接丢失

我有一个进程使用logging.SyslogHandler通过TCP将日志发送到syslog服务器.不幸的是,如果由于某种原因重新启动系统日志服务器,该过程将停止发送日志,并且无法重新建立连接.

我想知道是否有人知道克服此行为的方法并强制logging.SyslogHandler重新建立连接.

使用处理程序的代码类似于:

import logging
import logging.handlers
import logging.config

logging.config.fileConfig('logging.cfg')
logging.debug("debug log message")
Run Code Online (Sandbox Code Playgroud)

logging.cfg:

[loggers]
keys=root,remote

[handlers]
keys=local,remote

[logger_remote]
qualname=remote
level=INFO
handlers=remote

[logger_root]
qualname=root
level=DEBUG
handlers=local

[handler_local]
class=handlers.StreamHandler
level=DEBUG
formatter=local
args=(sys.stdout,)

[handler_remote]
class=handlers.SysLogHandler
level=DEBUG
formatter=remote
args=(('localhost', 514), handlers.SysLogHandler.LOG_USER, 1)

[formatters]
keys=local,remote

[formatter_local]
format=%(module)s %(levelname)s %(message)s

[formatter_remote]
format=%(asctime)s %(message)s
Run Code Online (Sandbox Code Playgroud)

syslog服务器重启后我遇到的错误是:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/logging/handlers.py", line 866, in emit
    self.socket.sendall(msg)
  File "/usr/local/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)

我很感激任何见解.谢谢!

python tcp syslog

9
推荐指数
2
解决办法
1185
查看次数

我可以在构建后自动运行脚本吗?

使用 zc.recipe.egg 生成一些控制台脚本后,我希望在安装其他所有内容后,这些脚本实际上由 buildout自动运行

我知道 z3c.recipe.runscript 构建配方,但 AFAIK,它需要设置额外的路径(因此包导入不会失败)或执行生成的 Python(脚本)模块,这两者似乎都没有能力的。

有没有办法用 z3c.recipe.runscript 或其他方式来完成这个?

python buildout

3
推荐指数
1
解决办法
610
查看次数

为什么杀死这个子进程会引发 ProcessLookupError?

我不明白什么时候需要杀死子进程。

for package in server.packages:
    n = subprocess.Popen(['which', package], stdout=subprocess.DEVNULL)
    n.wait()
    if n.returncode != 0:
        n.kill()
        <some other code>
Run Code Online (Sandbox Code Playgroud)

我收到错误(使用 Python3):

ProcessLookupError: [Errno 3] No such process
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释我什么时候子进程自杀以及什么时候需要手动完成?

python subprocess kill python-3.3 python-3.4

3
推荐指数
1
解决办法
5464
查看次数

PyObjC“此应用程序不允许通知”

我正在尝试测试一个简单的 Python 脚本来发送 macOS 通知:

import UserNotifications

def notif_callback(err):
    print("Error in notification callback:",err)

def auth_callback(granted, err):
    print("Granted: ",granted,)
    print("Error in authorization request: ",err)

content=UserNotifications.UNMutableNotificationContent.alloc().init()
content.setTitle_("Test")
r=UserNotifications.UNNotificationRequest.requestWithIdentifier_content_trigger_('test_notification',content,None)


c=UserNotifications.UNUserNotificationCenter.currentNotificationCenter()

c.requestAuthorizationWithOptions_completionHandler_(0b111111,auth_callback)

c.addNotificationRequest_withCompletionHandler_(r,notif_callback)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行该程序时,出现以下错误

Granted: False
Error in authorization request:  Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
Error in notification callback: Error Domain=UNErrorDomain Code=1 "Notifications are not allowed for this application" UserInfo={NSLocalizedDescription=Notifications are not allowed for this application}
Run Code Online (Sandbox Code Playgroud)

我没有看到我的系统有任何通知授权,操作系统似乎自动拒绝了该请求。在系统偏好设置中,Python已被授予所有通知权限。我在这里缺少什么?

pyobjc objective-c python-3.7 macos-mojave

3
推荐指数
1
解决办法
865
查看次数

为什么截断 BytesIO 会搞砸?

在 OSX 上的 Python 3.5.1 上运行:

import io

b = io.BytesIO()

b.write(b'222')
print(b.getvalue())

b.truncate(0)
b.write(b'222')
print(b.getvalue())
Run Code Online (Sandbox Code Playgroud)

产生:

b'222'
b'\x00\x00\x00222'
Run Code Online (Sandbox Code Playgroud)

所以截断以BytesIO某种方式导致它开始在开头插入额外的零字节?为什么?

python bytesio python-3.x

2
推荐指数
1
解决办法
1420
查看次数

定制Plone Dexterity工厂创建子内容

我认为可以创建一个调用默认工厂的自定义Dexterity工厂,然后将一些子内容(在我的案例中基于Archetypes)添加到创建的"父"Dexterity内容中.

我创建和注册自定义工厂没有问题.

但是,无论我使用什么方法(创建AT子内容),从自定义工厂中尝试时子内容创建都会失败.

我已经尝试了从plone.api到invokeFactory以及直接实例化AT内容类的所有内容.

在大多数情况下,traceback显示底层的Plone/CMF代码尝试使用getToolByName获取portal_types工具并失败; 类似地,当尝试直接实例化AT类时,manage_afterAdd然后尝试访问reference_catalog,该失败.

有没有办法让这项工作?

python plone dexterity

1
推荐指数
1
解决办法
105
查看次数