首页 \ 问答 \ 在shebang路径(path in shebang)

在shebang路径(path in shebang)

我的电脑上有/ bin / bash(3.2)和/ usr / local / bin / bash(4.0)。 在$ PATH环境变量中,bash 4.0在bash 3.2之前。

在我的bash脚本shebang中,我不喜欢明确地使用:

#!/usr/local/bin/bash

我想找到基于$ PATH优先级的bash。 怎么做? 谢谢!


I have /bin/bash (3.2) and /usr/local/bin/bash (4.0) on my computer. In the $PATH environment variable, bash 4.0 is prior to bash 3.2.

in my bash script shebang, I don't like to explicitly use:

#!/usr/local/bin/bash

I'd like to find bash based on $PATH priority. How to do it? Thanks!


原文:https://stackoverflow.com/questions/5453877
更新时间:2022-10-13 06:10

最满意答案

第一

正如文档所述

原子性是数据库事务的定义属性。 atomic允许我们创建一个代码块,在该代码块中保证数据库的原子性。 如果代码块成功完成,则更改将提交到数据库。 如果存在异常,则回滚更改。

在您的示例中,如果emit_event正在执行某些操作,则需要使用atomic ,并且只有在所有emit_event函数调用和queryset.update成功完成时才需要执行此更新。 但是如果emit_event状态不会影响你的业务更新逻辑,那么atomic就是多余的,因为正如你所说的那样, update具有内部atomic

第二

查询集很懒惰。 这意味着当您迭代查询集时,将对其进行评估。 所以你需要做这样的事情。 回答最新评论

try:
    queryset = Model.objects.filter(a=1)
    item_ids = list(queryset.values_list('id', flat=True)) # store ids for later
    if item_ids: # optimzing here instead of queryset.count() so there won't be hit to DB
        with transaction.atomic():
            queryset.update(a=2) # queryset will [] after this.
            for item in Model.objects.filter(id__in=item_ids):  # <- new queryset which gets only updated objects
                item.emit_event('Updated')
except:
    logger.info('Exception')

看到我们在迭代它时获取新的查询集以获取更新的项目


First

As docs state

Atomicity is the defining property of database transactions. atomic allows us to create a block of code within which the atomicity on the database is guaranteed. If the block of code is successfully completed, the changes are committed to the database. If there is an exception, the changes are rolled back.

In your example you would need atomic if emit_event is doing something and you want this update to be done only if all emit_event function calls and queryset.update are successfull. But if states of emit_event does not affect your business logic of update, atomic here would be redundant because as you said yourself update has internal atomic.

Second

Querysets are lazy. Which means evaluation of queryset will be done when you are iterating over it. So you need to do something like this. Answering latest comment

try:
    queryset = Model.objects.filter(a=1)
    item_ids = list(queryset.values_list('id', flat=True)) # store ids for later
    if item_ids: # optimzing here instead of queryset.count() so there won't be hit to DB
        with transaction.atomic():
            queryset.update(a=2) # queryset will [] after this.
            for item in Model.objects.filter(id__in=item_ids):  # <- new queryset which gets only updated objects
                item.emit_event('Updated')
except:
    logger.info('Exception')

See there we make new queryset when iterating over it to get updated items

相关问答

更多
  • Django 1.1中的新功能 Counter.objects.get_or_create(name = name) Counter.objects.filter(name = name).update(count = F('count')+1) 或使用F表达式 : counter = Counter.objects.get_or_create(name = name) counter.count = F('count') +1 counter.save() New in Django 1.1 Coun ...
  • 如果我已正确理解您的问题,则无法在超类中调用更新方法。 如果是这样,那是因为你说错了。 方法如下: super(PollQuerySet,self).update(*args, **kwargs) 在python 3.x的情况下,类名和self成为可选参数。 所以上面的这一行可以缩短为 super().update(*args, **kwargs) If I have understood your question correctly, you are unable to call the upda ...
  • 我们可以遍历这些属性并更新这些值,然后调用.save()函数: def my_fancy_update(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) self.save() 然而,这将手动设置属性,所以有可能会有一些信号,触发器等附加,它们会更新和清除这些值。 setattr(..)是一个Python函数,使setattr(x, 'y', z)等价于xy = z (注意,我们在setattr(..)中将'y' ...
  • 您正确使用transaction.atomic() (包括将try ... except在事务外部)但您绝对不应该设置AUTOCOMMIT = False 。 正如文档所述,当你想要“禁用Django的事务管理”时,你将系统范围的设置设置为False - 但这显然不是你想要做的,因为你正在使用transaction.atomic() ! 更多来自文档 : 如果你这样做,Django将不会启用自动提交,也不会执行任何提交。 您将获得底层数据库库的常规行为。 这要求您明确提交每个事务,甚至是由Django或第三 ...
  • 我知道为什么现在。 因为我的数据库引擎是MyISAM,所以这就是Transaction无效的原因。 人们使用Transaction必须注意,如果DB支持它! 参考: https : //docs.djangoproject.com/en/1.8/topics/db/transactions/#savepoints I know why now. Because my DB engine is MyISAM, that is why Transaction did not work. people use T ...
  • 现在,今天早上它没有任何变化。 关闭作为heisenbug。 And now, this morning it is working with no changes. Closing as a heisenbug.
  • 你不需要decorator @transaction.atomic和with atomic.transaction() ,一个通常就足够了。 在使用with atomic.transaction() ,请捕获IntegrityError异常,而不是一次性广泛处理所有异常。 编辑:如果您正在处理原子块之外的异常(如下所示),那么最好还有一个外部原子包装,用于处理异常处理部分中可能需要的回滚和其他数据库操作。 from django.db import IntegrityError, transaction d ...
  • 第一 正如文档所述 原子性是数据库事务的定义属性。 atomic允许我们创建一个代码块,在该代码块中保证数据库的原子性。 如果代码块成功完成,则更改将提交到数据库。 如果存在异常,则回滚更改。 在您的示例中,如果emit_event正在执行某些操作,则需要使用atomic ,并且只有在所有emit_event函数调用和queryset.update成功完成时才需要执行此更新。 但是如果emit_event状态不会影响你的业务更新逻辑,那么atomic就是多余的,因为正如你所说的那样, update具有内部a ...
  • 使用transaction.atomic decorator,而不是transaction.non_atomic_requests - 通过使用您告诉Django的transaction.non_atomic_requests ,此请求不应包含在事务中(每个更改都自动提交到数据库) Use transaction.atomic decorator, not transaction.non_atomic_requests - by using transaction.non_atomic_requests y ...
  • 您的方法签名错误: def pc_add_1(self, request, user, queryset): 对于管理员行动应该是 def pc_add_1(self, request, queryset): 现在,如果您想知道哪个管理员用户正在进行更改,您可以从request.user找到它 You have the method signature wrong: def pc_add_1(self, request, user, queryset): For admin actions it sh ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)