我真的在努力应对这整个应用程序的想法.我阅读了很多教程和样式指南,我知道我应该尝试创建专门的应用程序,这只做一件事.在查看一些简单的教程项目时,这一切都很有意义,但一旦进入复杂的现实生活项目,我发现自己无法确定应该如何划分不同应用程序之间的界限.
其中一个问题是,我希望有一个站点(或多个站点),用户可以看到很多不同的东西.遵循应用程序设计规则时应该来自不同应用程序的东西.我怎么会意识到这样的事情?我的第一个想法是创建一个名为ui的应用程序,它只处理实际导致模板的所有视图,所有其他应用程序提供模型和辅助功能.但我担心该ui应用程序会变得很大.
举个小例子:让我想拥有一个用户可以执行以下任务的站点:
现在,我会创建三个应用程序:
但是,我需要某种main或ui应用程序来处理这些应用程序的交互方式以及创建实际网站,其中所有应用程序都以某种方式参与其中.
那么,有没有"正确"的方法来做到这一点?或者我可以使用任何模式吗?我也很欣赏有关这个主题的良好资源的链接,尽管我已经阅读了不少内容.
我最近阅读了很多关于JavaScript排序的答案,我经常偶然发现一个比较函数,如下所示:
array.sort(function(a,b){ a > b ? 1 : -1; });
Run Code Online (Sandbox Code Playgroud)
所以它是一个比较函数,如果a大于则返回1,如果小于OR EQUAL TO b则返回-1 .如MDN(链接)所述,比较函数也可以返回零,以确保两个项目的相对位置保持不变:ab
如果compareFunction(a,b)返回0,则保持a和b相对于彼此保持不变,但是对于所有不同的元素进行排序.
所以官方的例子看起来更像是这样的:
function compare(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
实际上,通过添加return 0语句,排序算法通常需要更少的迭代并且总计运行得更快(JSPerf).
所以我想知道在省略return 0声明方面是否有任何优势.
我意识到在MDN上,它还说:
注意:ECMAscript标准不保证这种行为,因此并非所有浏览器(例如可追溯到至少2003年的Mozilla版本)都尊重这一点.
指的是行为,这a和b如果返回0应保持不变.那么也许,通过返回0,我们在不同的浏览器中得到一个稍微不同的排序数组?这可能是个原因吗?是否有任何其他充分理由不返回零?
TLDR;
要为celery生成的每个进程运行初始化函数,您可以使用该worker_process_init信号.正如您可以在文档中看到的那样,该信号的处理程序不应该阻塞超过4秒.但是有什么选择,如果我必须运行一个执行时间超过4秒的init函数?
问题
我使用C扩展模块在芹菜任务中运行某些操作.此模块需要初始化,可能需要几秒钟(可能是4-10).因为我宁愿不为每个任务运行这个init函数,但对于每个生成的进程,我都使用了这个worker_process_init信号:
#lib.py
import isclient #c extension module
client = None
def init():
global client
client = isclient.Client() #this might take a while
def create_ne_list(text):
return client.ne_receiventities4datachunk(text)
#celery.py
from celery import Celery
from celery.signals import worker_process_init
from lib import init
celery = Celery(include=[
'isc.ne.tasks'
])
celery.config_from_object('celeryconfig')
@worker_process_init.connect
def process_init(sender=None, conf=None, **kwargs):
init()
if __name__ == '__main__':
celery.start()
#tasks.py
from celery import celery
from lib import create_ne_list as cnl
@celery.task(time_limit=1200)
def create_ne_list(text):
return …Run Code Online (Sandbox Code Playgroud) python initialization multiprocessing python-c-extension celery
我正在使用getUserMedia API在浏览器中录制音频,然后将此音频发送到websocket服务器.此外,为了测试录音,我在Mac上使用soundflower作为输入设备,因此我可以播放波形文件,而不是说话麦克风.
客户端(JavaScript)
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var audioContext = new AudioContext();
var wsClient = new WebSocket("ws://" + WEBSOCKET_URL + ":" + WEBSOCKET_PORT);
navigator.getUserMedia({audio: true}, function (stream) {
var input = audioContext.createMediaStreamSource(stream);
var recordNode = audioContext.createScriptProcessor(4096);
recordNode.onaudioprocess = recorderProcess;
input.connect(recordNode);
recordNode.connect(audioContext.destination);
}, function (e) {
console.error("No live audio input: " + e);
});
function recorderProcess(e) {
var buffer = e.inputBuffer.getChannelData(0);
wsClient.send(buffer);
}
Run Code Online (Sandbox Code Playgroud)
服务器端(python)
在服务器端,我只是在文件中写入块:
def onMessage(self, msg, …Run Code Online (Sandbox Code Playgroud) 情况
假设我有一个指令,它必须通过ID在定义指令的元素内访问某些元素.可能出现的问题是,在评估指令时,子元素还没有.结果是,我无法通过其ID访问这些元素.
例
<div ng-controller="MyCtrl">
<div color="elementId">
<div ng-repeat="item in items" id="{{ item.id }}">
{{ item.name }}
</div>
</div>
</div>
<script>
var myApp = angular.module('myApp',[]);
myApp.directive("color", function () {
return {
restrict: "A",
link: function (scope, element, attributes) {
var name = attributes.color,
el = element[0];
scope.$watch(name, function () {
var id = scope[name];
console.log(id); //id1
console.log(element.children().eq(0).attr("id")); //{{ item.id }}
element.find("#"+id).css("background-color","red");
});
}
};
});
function MyCtrl($scope) {
$scope.items = [
{ id:"id1", name:"item1" },
{ id:"id2", name:"item2" } …Run Code Online (Sandbox Code Playgroud) 我正在使用ng-if来显示和隐藏元素.当元素出现时,我想调用一个服务,该服务在新元素中滚动到某个孩子(通过Id).问题是,如果我在将元素设置为可见之后尝试立即调用我的服务函数,那么DOM似乎还没有准备就绪.
var myApp = angular.module('myApp',[]);
myApp.factory("ScrollService", function () {
return {
scroll: function (id) {
console.log(document.getElementById(id));
}
};
});
function MyCtrl($scope, ScrollService) {
$scope.visible = false;
$scope.toggleVisibility = function () {
$scope.visible = !$scope.visible;
if ($scope.visible) {
ScrollService.scroll("myId"); //output: null
}
};
}
Run Code Online (Sandbox Code Playgroud)
document.getElementById()总会导致null.
这也是一个小提琴,它演示了这个问题:http://jsfiddle.net/Dpuq2/
那么有什么办法可以在ng-if操作之后立即触发一个函数吗?
编辑
使用MinkoGechev的小提琴,我能够在更真实的环境中重现我的错误,并使用指令而不是服务:FIDDLE
问题似乎发生了,因为我ng-repeat在ng-if-container中使用:
<div ng-controller="MyCtrl">
<div ng-if="visible">
<div id="myId" data-scroll="itemId">
<div id="xy"></div>
<div ng-repeat="item in items" id="{{ item.number }}">{{ item.number }}</div>
</div>
</div> …Run Code Online (Sandbox Code Playgroud) You can use celery to call a task by name, that is registered in a different process (or even on a different machine):
celery.send_task(task_name, args=args, kwargs=kwargs)
Run Code Online (Sandbox Code Playgroud)
(http://celery.readthedocs.org/en/latest/reference/celery.html#celery.Celery.send_task)
I now would like to be able to add a callback that will be executed as soon as the task finished and that will be executed within the process that is calling the task.
My Setup
I have a server A, that runs a django powered website and I use a …
在下面的一段代码中,我如何访问A.prototype.log内部B.prototype.log?
function A() {}
A.prototype.log = function () {
console.log("A");
};
function B() {}
B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;
B.prototype.log = function () {
//call A.prototype.log here
console.log("B");
};
var b = new B();
b.log();
Run Code Online (Sandbox Code Playgroud)
我知道我可以写,A.prototype.log.call(this)但我想也许有更优雅的方式,让我以相对的方式调用它,比如"调用原型链中下一个更高实例的方法'日志'".这样的事情可能吗?
假设我有两个简单的模型:
class Place(models.Model):
name = models.CharField(max_length=200)
address = models.CharField(max_length=200)
class Event(models.Model):
name = models.CharField(max_length=200)
date = models.DateField()
place = models.ForeignKey(Place)
Run Code Online (Sandbox Code Playgroud)
我现在想做的是查询一组已解析位置的事件,并将所有内容转换为字典列表,稍后可以将其转换为 JSON。
最终结果应如下所示:
[{
"name": "event1",
"date": "date1",
"place": {
"name": "place1",
"address": "address1",
},
},{
"name": "event2",
"date": "date2",
"place": {
"name": "place2",
"address": "address2",
},
},]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我尝试了我的运气ValueQuerySet和.values()方法:
Event.objects.all().select_related("place").values()
Run Code Online (Sandbox Code Playgroud)
但这是行不通的,因为.values()只返回相关对象的 id,而不返回内容。所以我想知道是否有另一种内置方法可以进行这种转换,或者我是否必须迭代 QuerySet 对象并自己进行转换。
我定义了一些时间限制为1200的任务:
@celery.task(time_limit=1200)
def create_ne_list(text):
c = Client()
return c.create_ne_list(text)
Run Code Online (Sandbox Code Playgroud)
每次新进程启动时,我还使用worker_process_init信号进行一些初始化:
@worker_process_init.connect
def init(sender=None, conf=None, **kwargs):
init_system(celery.conf)
init_pdf(celery.conf)
Run Code Online (Sandbox Code Playgroud)
该初始化函数需要几秒钟的时间来执行。
除此之外,我正在使用以下配置:
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'amqp://'
CELERY_TIMEZONE = 'Europe/Berlin'
CELERY_ENABLE_UTC = True
Run Code Online (Sandbox Code Playgroud)
并使用以下命令启动我的工作人员:
celery -A isc worker -l info --concurrency=3
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,启动工作程序会导致初始化函数被调用三次。现在,我可以发送任务并且它们正在执行,一切似乎都运行顺利。
但是:一旦任务超过其时间限制,工作人员就会陷入无限循环,因为超过时间限制而再次产生并被杀死。
[2014-06-13 09:46:18,978: ERROR/MainProcess] Timed out waiting for UP message from <Worker(Worker-20381, started daemon)>
[2014-06-13 09:46:20,000: ERROR/MainProcess] Process 'Worker-20381' pid:18953 exited with 'signal 9 (SIGKILL)'
// new worker 20382 …Run Code Online (Sandbox Code Playgroud) javascript ×5
python ×4
celery ×3
django ×3
angularjs ×2
audio ×1
django-apps ×1
dom ×1
getusermedia ×1
inheritance ×1
json ×1
overriding ×1
prototype ×1
sorting ×1
timeout ×1