首页 \ 问答 \ 确定javascript异步操作的结束(determining the end of asynchronous operations javascript)

确定javascript异步操作的结束(determining the end of asynchronous operations javascript)

如果我有一个传递了这个函数的函数:

function(work) {
   work(10);
   work(20);
   work(30);
}

(可以有任意数量的work电话,其中包含任何数字。)

work性能一些异步活动 - 比如,对于这个例子,它只是一个timeout 。 我完全可以控制完成此操作的工作(事实上,它的定义一般)。

确定所有work通话何时完成的最佳方法是什么?


我的当前方法在调用工作时递增计数器,在完成时递减计数器,并在计数器为0时触发all work done事件(在每次递减后检查)。 但是,我担心这可能是某种竞争条件。 如果不是这样,请告诉我原因,这将是一个很好的答案。


If I have a function that's passed this function:

function(work) {
   work(10);
   work(20);
   work(30);
}

(There can be any number of work calls with any number in them.)

work performance some asynchronous activity—say, for this example, it just is a timeout. I have full control over what work does on the completion of this operation (and, in fact, its definition in general).

What's the best way of determining when all the calls to work are done?


My current method increments a counter when work is called and decrements it when it completes, and fires the all work done event when the counter is 0 (this is checked after every decrement). However, I worry that this could be a race condition of some sort. If that is not the case, do show my why and that would be a great answer.


原文:https://stackoverflow.com/questions/6852059
更新时间:2022-04-04 07:04

最满意答案

这是另一个版本,它带来了显着的改进:

   function cohens_kappa2(x::Vector{Int}, k::Int)
     d = Dict{Int,Int}()
     n = length(x)
     c1 = Int[]
     pnew = 0
     for i=1:n
       p = get(d,x[i],0)
       if p>0
         c1[p] += 1
       else
         pnew += 1
         d[x[i]] = pnew
         push!(c1,1)
       end
     end
     c2 = zeros(Int,pnew)
     for i=(k+1):n
       if x[i-k]==x[i] c2[d[x[i]]] += 1 ; end
     end
     num, dentmp = 0.0, 0.0
     for i=1:pnew
       pjjk = c2[i]/(n-k)
       pj = c1[i] / n
       num += pjjk - pj^2
       dentmp += pj^2
     end
     return (num / (1.0-dentmp))
   end

一般来说,几乎总是可以进行优化,但就像从自然界中提取石油一样,它会增加程序员的成本和工作量。

在一个测试用例中,上面给了我5倍到10倍的加速。 您的数据结果如何?


Here is another version, which gives a significant improvement:

   function cohens_kappa2(x::Vector{Int}, k::Int)
     d = Dict{Int,Int}()
     n = length(x)
     c1 = Int[]
     pnew = 0
     for i=1:n
       p = get(d,x[i],0)
       if p>0
         c1[p] += 1
       else
         pnew += 1
         d[x[i]] = pnew
         push!(c1,1)
       end
     end
     c2 = zeros(Int,pnew)
     for i=(k+1):n
       if x[i-k]==x[i] c2[d[x[i]]] += 1 ; end
     end
     num, dentmp = 0.0, 0.0
     for i=1:pnew
       pjjk = c2[i]/(n-k)
       pj = c1[i] / n
       num += pjjk - pj^2
       dentmp += pj^2
     end
     return (num / (1.0-dentmp))
   end

In general, optimization is almost always possible, but like extracting oil from nature, it comes with growing costs and effort for the programmer.

On a test case the above gave me 5x to 10x speedup. What is the result for your data?

相关问答

更多
  • using Plots plot(x, predict(fit1), seriestype = :line) #where x is your predictor variable 更新以包含@pkofod的评论 using Plots plot(x, predict(fit1), seriestype = :line) #where x is your predictor variable Updated to include @pkofod 's comment
  • 如果你在谈论普通的朱莉娅变量 c = rand(5,3) x = rand(5,3) @show sum(c.*x) 但是,如果你指的是JuMP(根据你以前的问题),那么使用sum{} : using JuMP m = Model() @variable(m, 0 <= x[i=1:5,j=1:3] <= 1) c = rand(5,3) @constraint(m, sum{c[i,j]*x[i,j],i=1:5,j=1:3} <= 10) If you are talking about plain ...
  • 目前,Julia JIT在启动时编译了整个标准库。 我们意识到这种情况,目前正在缓存LLVM JIT输出以补救这种情况,但直到那时,它就没有办法了(除了使用REPL)。 At the moment Julia JIT compiles its entire standard library on startup. We are aware of the situation and are currently working on caching the LLVM JIT output to remedy t ...
  • 很好找到问题...我的dll文件是32和julia是64.所以要么安装32位版本的Julia或获得64位版本的.dll文件来推进。 函数按路径查找文件: ccall( (:fun, "C:\\Users\\pinq-\\Documents\\Julia\\librarie"), Int32, ()) Well found the problem... My dll file was 32 and julia was 64. So either install 32-bit version of Julia ...
  • 因此,首先,我删除了plot_probs的最后两行 - 绘图对于我认为的基准测试来说并不是一件好事,而且它基本上超出了我的(或您的)控制范围 - 如果它是一个真正的因素,可以尝试PyPlot。 我还plot_probs了几次plot_probs ,看看第一次花多少时间编译它: **********elapsed time: 1.071184218 seconds (473218720 bytes allocated, 26.36% gc time) **********elapsed time: 0.658 ...
  • 这是另一个版本,它带来了显着的改进: function cohens_kappa2(x::Vector{Int}, k::Int) d = Dict{Int,Int}() n = length(x) c1 = Int[] pnew = 0 for i=1:n p = get(d,x[i],0) if p>0 c1[p] += 1 else pnew += 1 ...
  • @which正在寻找你: @which (1:10) * ones(1, 10) # *(A::AbstractArray{T,1}, B::AbstractArray{T,2}) at linalg/matmul.jl:89 在Jupyter中 ,它还将超链接到相应的代码行,其中该方法在Julia的GitHub中定义。 @which is what you are looking for: @which (1:10) * ones(1, 10) # *(A::AbstractArray{T,1}, B: ...
  • 来自http://docs.julialang.org/en/release-0.4/manual/constructors/ 内部构造方法 虽然外部构造方法成功地解决了为构造对象提供额外的便利方法的问题,但是它们无法解决本章介绍中提到的另外两个用例:强制不变量,并允许构造自引用对象。 对于这些问题,需要内部构造方法。 内部构造函数方法很像外部构造函数方法,有两个不同之处: 它在类型声明的块中声明,而不是像普通方法一样在它之外声明。 它可以访问一个名为new的特殊本地存在函数,该函数创建块类型的对象。 Fr ...
  • Julia中的函数返回它们中的最后一个表达式。 在这种情况下,它是data = 1 ,即返回data而不是MyData的新实例。 只需在data = 1之后添加new(data)行以返回MyData新实例,它就能正常工作。 Functions in Julia return the last expression in them. In this case, it is data = 1, that is data is returned instead of new instance of MyData. ...
  • 使用Iterators包,它可能如下所示: using Iterators for p in product([1,2], [3,4]) println(p) end 在哪里用算法替换println 。 如果获得所有组合的集合很重要,您也可以使用collect 。 Using the Iterators package, it might look like this: using Iterators for p in product([1,2], [3,4]) println(p) en ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)