我试图atexit在一个Process,但不幸的是它似乎没有用.这是一些示例代码:
import time
import atexit
import logging
import multiprocessing
logging.basicConfig(level=logging.DEBUG)
class W(multiprocessing.Process):
def run(self):
logging.debug("%s Started" % self.name)
@atexit.register
def log_terminate():
# ever called?
logging.debug("%s Terminated!" % self.name)
while True:
time.sleep(10)
@atexit.register
def log_exit():
logging.debug("Main process terminated")
logging.debug("Main process started")
a = W()
b = W()
a.start()
b.start()
time.sleep(1)
a.terminate()
b.terminate()
Run Code Online (Sandbox Code Playgroud)
此代码的输出是:
DEBUG:root:Main process started DEBUG:root:W-1 Started DEBUG:root:W-2 Started DEBUG:root:Main process terminated
我希望的是,W.run.log_terminate()将被调用时a.terminate()和b.terminate()被调用,并且输出被什么东西likeso(强调)!:
DEBUG:root:Main process started DEBUG:root:W-1 Started DEBUG:root:W-2 Started …
我正在尝试创建一个简单的调试函数,只显示函数的调用者,如下所示:
function xe() {
console.log(xe.caller().name)
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我只能添加xe()一个函数,它将记录对函数的调用 - 只是一个简单的简单添加,以帮助调试.调试糖,可以这么说.
不幸的是我从主题行得到错误:
TypeError:'caller'和'arguments'是受限制的函数属性,在此上下文中无法访问.
我正在使用Babel/ES6,它注入"use strict"每个模块的顶部.这可能是原因,但搜索已经产生了有关错误引发原因的有限信息,我想更好地理解它.
如果严格模式是问题,我宁愿不为整个项目禁用严格模式 - 仅用于模块/功能.
给定一个带有模块名称的字符串,如何导入模块中的所有内容,就像调用了:
from module import *
Run Code Online (Sandbox Code Playgroud)
即给定字符串S ="模块",如何得到相当于以下内容:
__import__(S, fromlist="*")
Run Code Online (Sandbox Code Playgroud)
这似乎没有按预期执行(因为它不会导入任何东西).
我发现它pylint很有用,但我也发现它非常没有文档记录,输出结构很痛苦,缺乏直观的界面.
我想使用pylint,但它不断抽出一些荒谬的"常规"消息,例如C: 2: Line too long (137/80)等等.
如果我可以禁用这些,那么pylint对我来说会更有用.如何禁用这些"约定"消息?
我试图把disable-msg=C301在~/.pylintrc(它被加载,因为当我把有错误pylint抱怨),我的理解是基于pylint的包目录下运行此命令(文档中的"行太长"的消息,可以发现会对人好点):
$ grep"行太长"**/*.py checkers/format.py:'C0301':('行太长(%s /%s)',
但这disable-msg没有任何作用.我将convention使用该disable-msg-cat=命令禁用整个类别,但是没有任何迹象表明我可以找到convention该命令的类别标识符- 直观disable-message-cat=convention无效.
在这个问题上,我非常有必要指出一些方向.
谢谢.
布赖恩
我有一个Jinja2字典,我想要一个修改它的表达式 - 通过改变它的内容,或者与另一个字典合并.
>>> import jinja2
>>> e = jinja2.Environment()
Run Code Online (Sandbox Code Playgroud)
修改字典:失败.
>>> e.from_string("{{ x[4]=5 }}").render({'x':{1:2,2:3}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "jinja2/environment.py", line 743, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "jinja2/environment.py", line 469, in compile
self.handle_exception(exc_info, source_hint=source)
File "<unknown>", line 1, in template
jinja2.exceptions.TemplateSyntaxError: expected token
'end of print statement', got '='
Run Code Online (Sandbox Code Playgroud)
两阶段更新:打印多余的"无".
>>> e.from_string("{{ x.update({4:5}) }} {{ x }}").render({'x':{1:2,2:3}})
u'None {1: 2, 2: 3, 4: 5}'
>>> e.from_string("{{ dict(x.items()+ {3:4}.items()) }}").render({'x':{1:2,2:3}}) …Run Code Online (Sandbox Code Playgroud) 给定两个Javascript对象(A和B),有没有办法生成JSON补丁,以便在应用该补丁A时将对象的属性更改为对象的属性B?
例如,给定假设JSONPatch函数(可能是与下面链接的函数之一相似的名称的函数),期望的是generate_patch函数.
patch = generate_patch(A, B)
JSONPatch.apply(patch, A) # modifies A so that it has the same properties as B.
在这个问题A,B是Javascript对象.RFC6902创建的补丁是JSON,它表示应用于A该对象的操作数组B.该generate_patch函数不需要返回JSON,而是为了提高效率可以返回一个Javascript对象,该对象在JSON.stringify调用时成为RFC6902 JSON补丁文档.
我在这个主题上找到的项目是:
我有一个运行Django的Python脚本用于数据库和内存缓存,但它特别是作为一个独立的守护进程运行(即不响应webserver请求).守护进程检查带有a的对象的Django模型申请单status=STATUS_NEW,然后将它们标记为STATUS_WORKING并将它们放入队列中.
许多进程(使用多进程包创建)将从队列中提取出来并使用pr.id传递给队列的Requisition进行处理.我相信内存泄漏可能在下面的代码中(但它可能在Queue另一端的'Worker'代码中虽然这不太可能,因为即使没有请购单即将出现,内存大小也在增长 - 即当工作者都在Queue.get()上阻塞时.
from requisitions.models import Requisition # our Django model
from multiprocessing import Queue
while True:
# Wait for "N"ew requisitions, then pop them into the queue.
for pr in Requisition.objects.all().filter(status=Requisition.STATUS_NEW):
pr.set_status(pr.STATUS_WORKING)
pr.save()
queue.put(pr.id)
time.sleep(settings.DAEMON_POLL_WAIT)
Run Code Online (Sandbox Code Playgroud)
哪里settings.DAEMON_POLL_WAIT=0.01.
看来,如果我让它运行一段时间(即几天),Python进程将增长到无限大,最终系统将耗尽内存.
这里发生了什么(或者我怎么能找到),更重要的是 - 你怎么能运行这样做的守护进程?
我的第一个想法是改变函数的动态,特别是通过检查新的Requisition对象django.core.cache cache,即
from django.core.cache import cache
while True:
time.sleep(settings.DAEMON_POLL_WAIT)
if cache.get('new_requisitions'):
# Possible race condition
cache.clear()
process_new_requisitions(queue)
def process_new_requisitions(queue):
for pr in Requisition.objects.all().filter(status=Requisition.STATUS_NEW):
pr.set_status(pr.STATUS_WORKING)
pr.save()
queue.put(pr.id)
Run Code Online (Sandbox Code Playgroud)
创建申请单的过程status=STATUS_NEW可以执行cache.set('new_requisitions', …
在Jinja2中,如何{% set X=Y %}在合并的文件中访问分配的变量(即)include?
鉴于两个Jinja2文件,我希望以下工作:
A.jinja:
Stuff
{% include 'B.jinja' -%}
B has {{ N }} references
Run Code Online (Sandbox Code Playgroud)
B.jinja:
{% set N = 12 %}
Run Code Online (Sandbox Code Playgroud)
我希望A.jinja,当用Jinja2编译时,会产生以下输出:
Stuff
B has 12 references
Run Code Online (Sandbox Code Playgroud)
但是,它产生:
Stuff
B has references
Run Code Online (Sandbox Code Playgroud)
对于如何N在包含设置文件的文件中访问Jinja2变量(如上所述),我将非常感激N.
谢谢你的阅读.
布赖恩
我试图让VIM使用'='和相关命令缩进Javascript.当我尝试自动缩进以下内容时,例如:
new function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).setSelectionRange) {
$(this).setSelectionRange(pos, pos);
} else if ($(this).createTextRange) {
var range = $(this).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
Run Code Online (Sandbox Code Playgroud)
结果是相当荒谬的:
new function($) {
$.fn.setCursorPosition = function(pos) {
if ($(this).setSelectionRange) {
$(this).setSelectionRange(pos, pos);
} else if ($(this).createTextRange) {
var range = $(this).createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
Run Code Online (Sandbox Code Playgroud)
我已经设定了set syntax=javascript,我已经设定filetype为:
filetype detection:ON plugin:ON indent:ON
Run Code Online (Sandbox Code Playgroud)
虽然我已经尝试了每一种排列.我已经尝试了smartindent,autoindent和cindent,但似乎没有任何东西给Vim预期的缩进的正确效果.我已经定了tabstop=4.
我安装了javascript.vim, …
请记住,这里已经回答了类似的问题,我想知道data-bind='foreach: list'如果列表为空,如何在Knockout中使用默认文本或HTML显示.
链接页面上的解决方案似乎与此不相符,无论如何我想到了另一种尝试使用这样的自定义绑定来实现此目的的方法:
text.default = {
update: function (element, valueAccessor) {
var $e = $(element),
obs = valueAccessor();
function _check_blank() {
// the element has content - so we do nothing
if ($e.text().trim()) {
return;
}
// the element is empty;
$e.text("Default Text")
}
// we use setTimeout to ensure that any other bindings complete
// their update
setTimeout(_check_blank, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎与简单的observable相当好,但它不适用于foreach绑定,但无论如何我认为extender上述链接中的建议可能更好,原因有几个 - 上面的代码会有一些警告.尽管如此,我把这个例子放在这里,因为它有点突出了一个替代品和思考的食物.
所有这些,我想知道提供默认代替foreach内容可能有哪些选项.
一个是提供一个简单的包装器if,如下所示:
<!-- …Run Code Online (Sandbox Code Playgroud) python ×4
javascript ×3
jinja2 ×2
atexit ×1
auto-indent ×1
daemon ×1
dictionary ×1
django ×1
ecmascript-6 ×1
import ×1
include ×1
indentation ×1
json ×1
json-patch ×1
knockout-2.0 ×1
knockout.js ×1
memory-leaks ×1
merge ×1
pylint ×1
strict ×1
terminate ×1
vim ×1