我有一个CPU密集型Celery任务.我想在许多EC2实例中使用所有处理能力(核心)来更快地完成这项工作(我认为芹菜并行分布式多任务处理任务).
术语,线程,多处理,分布式计算,分布式并行处理都是我试图更好理解的术语.
示例任务:
@app.task
for item in list_of_millions_of_ids:
id = item # do some long complicated equation here very CPU heavy!!!!!!!
database.objects(newid=id).save()
Run Code Online (Sandbox Code Playgroud)
使用上面的代码(如果可能的话,有一个例子)如何使用Celery分配这个任务,允许利用云中所有可用机器的所有计算CPU功率来分离这一任务?
我是django的新手并使用构建REST API django-rest-framework.我已经编写了一些代码来检查用户是否提供了一些参数.但是这很难看if conditions,所以我想重构它.Below是我写的代码请建议如何重构它.
我正在寻找一些基于django的验证.
class AssetsViewSet(viewsets.ModelViewSet):
queryset = Assets.objects.using("gpr").all()
def create(self, request):
assets = []
farming_details = {}
bluenumberid = request.data.get('bluenumberid', None)
if not bluenumberid:
return Response({'error': 'BlueNumber is required.'})
actorid = request.data.get('actorid', None)
if not actorid:
return Response({'error': 'Actorid is required.'})
asset_details = request.data.get('asset_details', None)
if not asset_details:
return Response({'error': 'AssetDetails is required.'})
for asset_detail in asset_details:
location = asset_detail.get('location', None)
if not location:
return Response({'error': 'location details is required.'})
assettype = asset_detail.get('type', None)
if …Run Code Online (Sandbox Code Playgroud) 我有一个Django应用程序,每隔一段时间就会进入内存泄漏.
我没有使用可能使内存过载的大数据,事实上应用程序会逐渐"吃掉"内存(在一周内内存从大约70 MB到4GB),这就是为什么我怀疑垃圾收集器丢失的东西,我是虽然不确定.此外,似乎此增量不依赖于请求的数量.
明显的事情,例如DEBUG=True,保留打开的文件等......这里不适用.
我正在使用uWSGI 2.0.3(+ nginx)和Django 1.4.5
我就可以建立wsgi这样,当内存超过一定限制时重新启动服务器,但我不喜欢这样做,因为这是不是一个真正的解决方案.
是否有任何众所周知的垃圾收集器"无法正常工作"的情况?它能提供一些代码示例吗?
是否有任何可能导致这种情况的uWSGI + Django配置?
让我们假设有一行代码使用包含很长"查找名称"的Django ORM来执行查询:
QuerySet.filter(myfk__child__onetoone__another__manytomany__relation__monster__relationship__mycustomlookup=':P')
Run Code Online (Sandbox Code Playgroud)
我知道我们可以这样做:
QuerySet.filter(
**{
'myfk__child__onetoone__another'
'__manytomany__relation__monster'
'__relationship__mycustomlookup': ':P'
}
)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否还有另一种,也许更多的pythonic /接受的方式?
这主要是一个逻辑问题,但上下文是在Django中完成的.
在我们的数据库中,我们有Vertex和Line Classes,它们形成一个(神经)网络,但它是无序的,我无法改变它,它是一个遗留数据库
class Vertex(models.Model)
code = models.AutoField(primary_key=True)
lines = models.ManyToManyField('Line', through='Vertex_Line')
class Line(models.Model)
code = models.AutoField(primary_key=True)
class Vertex_Line(models.Model)
line = models.ForeignKey(Line, on_delete=models.CASCADE)
vertex = models.ForeignKey(Vertex, on_delete=models.CASCADE)
Run Code Online (Sandbox Code Playgroud)
现在,在应用程序中,用户将能够直观地选择两个顶点(下面的绿色圆圈)
然后javascript将这两个顶点的pk发送到Django,它必须找到满足它们之间路由的Line类,在这种情况下,以下4个红线:
商业逻辑:
到目前为止我所拥有的:
我知道基本逻辑是循环遍历每个Vertex的所有行,然后得到这些行的另一个Vertex,并继续递归,但我真的不知道从哪里开始.
这是我可以得到的,但它可能没有帮助(views.py):
def findRoute(request):
data = json.loads(request.body.decode("utf-8"))
v1 = Vertex.objects.get(pk=data.get('v1_pk'))
v2 = Vertex.objects.get(pk=data.get('v2_pk'))
lines = v1.lines.all()
routes = []
for line in lines:
starting_line = line
#Trying a new route
this_route_index = len(routes)
routes[this_route_index] = [starting_line.pk]
other_vertex = line.vertex__set.all().exclude(pk=v1.pk)
#There are …Run Code Online (Sandbox Code Playgroud) 我想知道当我们需要在django项目中使用某种任务队列时需要考虑什么标准,我正在考虑性能,开发速度,灵活性等.
我一直在使用Celery + RabbitMQ和Django-ztask + ZeroMQ一段时间(我确定还有其他好的),但我没有一个准确的佳能来获取最适合每种情况.
您是否可以为每个允许用户在它们之间选择的特性提供一些特性?它是否可能包含其他一些稳定的MQ方法?
我查看了与我类似的各种问题,但我找不到任何解决问题的方法.
在我的代码中,我想在一个名为files的文件夹中提供一个新生成的excel文件,该文件位于我的app目录中
excelFile = ExcelCreator.ExcelCreator("test")
excelFile.create()
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="test.xls"'
return response
Run Code Online (Sandbox Code Playgroud)
因此,当我单击运行此部分代码的按钮时,它会向用户发送一个空文件.通过查看我的代码,我可以理解这种行为,因为我没有在我的回复中指向该文件...
我看到有些人使用文件包装器(我不太了解它的使用).所以我喜欢这样:
response = HttpResponse(FileWrapper(excelFile.file),content_type='application/vnd.ms-excel')
Run Code Online (Sandbox Code Playgroud)
但是,我从服务器收到错误消息:发生服务器错误.请联系管理员.
感谢您帮助我完成我的Django任务,我的所有宝贵建议都让您变得更好!
我想嘲笑时刻,以便能够设定一定的时间类型的字段DateTimeField用auto_now_add=True在我的测试,例如:
class MyModel:
...
created_at = models.DateTimeField(auto_now_add=True)
...
class TestMyModel(TestCase):
...
def test_something(self):
# mock current time so that `created_at` be something like 1800-02-09T020000
my_obj = MyModel.objects.create(<whatever>)
# and here my_obj.created_at == 1800-02-09T000000
Run Code Online (Sandbox Code Playgroud)
我知道当前日期总是用于这种类型的字段,这就是为什么我正在寻找一种替代方法来模拟系统时序,但只是在上下文中.
我已经尝试了一些方法,例如,创建一个上下文freeze_time但没有工作:
with freeze_now("1800-02-09"):
MyModel.objects.create(<whatever>)
# here the created_at doesn't fit 1800-02-09
Run Code Online (Sandbox Code Playgroud)
我想,这是因为当对象创建的方式背后的机制auto_now_add=True.
我不想删除auto_now_add=True并使用默认值.
有没有办法我们可以模拟时间,以便我们可以做到这一点,这种领域在某些情况下得到我想要的时间?
我正在使用Django 1.9.6和Python 3.4
我正在使用以下表格:
class InvoiceModelForm ( forms.ModelForm ):
u = forms.ModelChoiceField ( queryset = User.objects.all () )
Run Code Online (Sandbox Code Playgroud)
但是在表单字段中显示用户名.我想改变它以使用名字和姓氏.
我不想改变
def __str__(self):
return self.username
Run Code Online (Sandbox Code Playgroud)
如何更改表单字段中显示的值?
我有这样的代码:
@login_required
def download_file(request):
content_type = "application/octet-stream"
download_name = os.path.join(DATA_ROOT, "video.avi")
with open(download_name, "rb") as f:
wrapper = FileWrapper(f, 8192)
response = HttpResponse(wrapper, content_type=content_type)
response['Content-Disposition'] = 'attachment; filename=blabla.avi'
response['Content-Length'] = os.path.getsize(download_name)
# response['Content-Length'] = _file.size
return response
Run Code Online (Sandbox Code Playgroud)
似乎它有效。但是,如果我下载更大的文件(例如~600MB),我的内存消耗会增加 600MB。经过几次这样的下载后,我的服务器抛出:
内部服务器错误:/download/ 回溯(最近一次调用最后一次):
文件“/home/matous/.local/lib/python3.5/site-packages/django/core/handlers/exception.py”,第 35 行,在内部响应 = get_response(request) 文件“/home/matous/. local/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/matous/.local/lib/ python3.5/site-packages/django/core/handlers/base.py”,第 126 行,在 _get_response 响应 =wrapped_callback(request, *callback_args, **callback_kwargs) 文件“/home/matous/.local/lib/python3 .5/site-packages/django/contrib/auth/decorators.py”,第 21 行,在 _wrapped_view 中返回 view_func(request, *args, **kwargs) 文件“/media/matous/89104d3d-fa52-4b14-9c5d- 9ec54ceebebb/home/matous/phd/emoapp/emoapp/mainapp/views.py",第 118 行,在 download_file …