Rails:在停止之前验证该开始,DateTime(Rails: Validating that start is before stop, DateTime)
我正在尝试使用
validates_timeliness
来确保SliderImage.start
总是在SliderImage.stop
之前:class SliderImage < ActiveRecord::Base validates_datetime :start, :stop validates :start, :timeliness => {:before => stop} end
但当然,
stop
还没有定义。 我怎样才能做到这一点?I am trying to use
validates_timeliness
to ensure thatSliderImage.start
is always beforeSliderImage.stop
:class SliderImage < ActiveRecord::Base validates_datetime :start, :stop validates :start, :timeliness => {:before => stop} end
But of course
stop
is not defined yet. How can I accomplish this?
原文:https://stackoverflow.com/questions/8853983
最满意答案
如果您想要最可维护的选项,则只提供一个
async
API,它不会执行任何阻塞调用或使用任何线程池线程来实现。如果您真的想同时具有
async
和同步API,那么您将遇到可维护性问题。 你真的需要实现两次:一次async
,一次同步。 这两种方法看起来几乎相同,所以初始化实现很容易,但是最终会有两种分开的几乎相同的方法,所以维护是有问题的。特别是,没有一个很好的简单的方法来做一个
async
或同步的“包装”。 Stephen Toub有关于该主题的最佳信息:
- 我应该为同步方法公开异步包装器吗?
- 我应该为异步方法公开同步包装器吗?
(两个问题的简短答案是“否”)
If you want the most maintainable option, only provide an
async
API, which is implemented without making any blocking calls or using any thread pool threads.If you really want to have both
async
and synchronous APIs, then you'll encounter a maintainability problem. You really need to implement it twice: onceasync
and once synchronous. Both of those methods will look nearly identical so the initial implementation is easy, but you will end up with two separate nearly-identical methods so maintenance is problematic.In particular, there's no good and simple way to just make an
async
or synchronous "wrapper". Stephen Toub has the best info on the subject:
- Should I expose asynchronous wrappers for synchronous methods?
- Should I expose synchronous wrappers for asynchronous methods?
(the short answer to both questions is "no")
相关问答
更多-
您可以将它包装在Task.Run() ,如下所示,如果您希望它被异步调用: public override bool TestMethod() { var task = Task.Run(async () => { return await engine.DoAsyncFunction(); }); var Result = task.Result; // use returned result from async method here } You can w ...
-
如果您想要最可维护的选项,则只提供一个async API,它不会执行任何阻塞调用或使用任何线程池线程来实现。 如果您真的想同时具有async和同步API,那么您将遇到可维护性问题。 你真的需要实现两次:一次async ,一次同步。 这两种方法看起来几乎相同,所以初始化实现很容易,但是最终会有两种分开的几乎相同的方法,所以维护是有问题的。 特别是,没有一个很好的简单的方法来做一个async或同步的“包装”。 Stephen Toub有关于该主题的最佳信息: 我应该为同步方法公开异步包装器吗? 我应该为异步方法 ...
-
此外,突然CPU绑定计算变得异步,这不是它应该如何工作... 这背后有一个简单的原因:当人们看到异步方法时,他们通常认为,这个方法将异步执行,因此不会阻塞当前线程。 你的方法会这样: //Start... (On current thread) //Access the database (On IO Thread, freeing up the current thread) //Return to current thread //Do the calculations (O ...
-
回答我自己的问题: require 'em-hiredis' require 'sinatra/base' class App < Sinatra::Base def redis @redis ||= EM::Hiredis.connect end get '/' do stream :keep_open do |out| redis.blpop('abcdef', 15).callback do |x| out << "x=#{x}" ...
-
我怎样才能实现一个异步方法和它的同步对手?(How can I implement both an async method and its synchronous counterpart? [duplicate])[2021-08-21]
如果你的库需要实现同步和异步成员,那么你实现了两个成员。 没有捷径(假设这是一个可重用的库)。 public async TaskGetContentAsync(string url) { ... // Logic here, e.g., using HttpClient } public string GetContent(string url) { ... // Duplicate logic here, e.g., using WebClient } 逻辑的重复当然是不幸 ... -
如果您使用的是Web API,如果该方法不是真正的 async则不会使用async Log方法。 看看Stephen Cleary的这个答案。 If you're using Web API I would not go for an async Log method if the method is not truly async. Have a look at this answer from Stephen Cleary.
-
播放异步WS API解释(Play async WS API explained)[2022-10-01]
在引擎盖下播放使用基于NIO的async-http-client 。 将调度请求,并且当服务器响应时,将在来自线程池的线程上执行回调(在这种情况下,将来完成)。 这样就不会阻塞任何线程。 Under the hood play uses async-http-client which is based on NIO. A request will be dispatched and when the server responds, a callback (in this case the completi ... -
我的问题是我希望同步函数能够等待来自异步的值... 他们不能,因为: JavaScript基于由线程处理的“作业队列”,其中作业具有运行到完成语义,并且 JavaScript实际上并没有异步功能(真的 - 坚持这个......) 作业队列(事件循环)在概念上非常简单:当需要完成某些事情(初始执行脚本,事件处理程序回调等)时,该工作将被放入作业队列中。 为该作业队列提供服务的线程获取下一个待处理作业,将其运行至完成,然后返回下一个作业。 (当然,它比这更复杂,但这足以满足我们的目的。)因此,当一个函数被调用时 ...
-
正如@loteq所提到的,你可以简单地在Future上调用.actionGet()来使它同步。 检查elasticsearch集成测试 ,他们有一些非常好的例子。 As @loteq mentioned, you can simply call .actionGet() on a Future to make it synchronous. Check elasticsearch integration tests, they have some very good examples.
-
根据OP中提供的链接文档,访问API应该在您的预期应用程序中看起来像这样。 public async Task
> TestAsync() { var client = new InfluxClient(new Uri("http://localhost:8086")); var users = await client.ShowUsersAsync(); return users; } 虽然OP中的简单示例使用控制台应用程序,但这里假设控 ...