为什么这些线程化的ActiveRecord查询不会同时运行?(Why won't these threaded ActiveRecord queries run concurrently?)
我正在尝试理解生产中的问题,所以我把这段代码扔到dev中的控制器动作中进行测试:
start = Time.now num_threads = 6 results = Queue.new saved_results = [] threads = [] connections = [] semaphore = Mutex.new # start threads (1..num_threads).each do |i| threads << Thread.new do #semaphore.synchronize { connections << ActiveRecord::Base.connection } # for cleanup? #ActiveRecord::Base.connection.execute("select sleep(1.6);") # runs sequentially sleep(1.6) # runs concurrently result = User.find_by_id(i) results << [i, result] end end # end option 1 - let everyone finish threads.each(&:join) # end option 2 - simulate early exit condition #while saved_results.count < 3 do saved_results << results.pop end #threads.each(&:exit) # cleanup/close open connections? #connections.select(&:active?).each(&:disconnect!) elapsed = Time.now - start render :text => [ elapsed.to_s, saved_results.size, results.size ].join(", ")
如预期的那样,
sleep(1.6)
在大约1.6
秒内执行。但是,ActiveRecord
select sleep(1.6);
尽管mysql控制台show processlist;
,但需要6 * 1.6 = 9.6
秒show processlist;
显示为每个线程打开的独立连接*。这是怎么回事? 为什么ActiveRecord查询不会同时运行? 我也在生产控制台中体验过这一点。
我确实有
config.threadsafe!
在config/environment.rb
设置。 如果重要,我正在使用Rails 2.3。*这些连接必须手动关闭? 生产总是有很多无用的开放连接,导致
Mysql::Error: Too many connections
。 我可能会将此问题作为另一个问题提交。I'm experimenting to understand a problem in production, so I've thrown this snippet inside a controller action in dev to test:
start = Time.now num_threads = 6 results = Queue.new saved_results = [] threads = [] connections = [] semaphore = Mutex.new # start threads (1..num_threads).each do |i| threads << Thread.new do #semaphore.synchronize { connections << ActiveRecord::Base.connection } # for cleanup? #ActiveRecord::Base.connection.execute("select sleep(1.6);") # runs sequentially sleep(1.6) # runs concurrently result = User.find_by_id(i) results << [i, result] end end # end option 1 - let everyone finish threads.each(&:join) # end option 2 - simulate early exit condition #while saved_results.count < 3 do saved_results << results.pop end #threads.each(&:exit) # cleanup/close open connections? #connections.select(&:active?).each(&:disconnect!) elapsed = Time.now - start render :text => [ elapsed.to_s, saved_results.size, results.size ].join(", ")
sleep(1.6)
executes in approximately1.6
seconds, as expected.However, the ActiveRecord
select sleep(1.6);
takes6 * 1.6 = 9.6
seconds, despite mysql consoleshow processlist;
displaying that independent connections are opened for each thread*.What's going on? Why won't the ActiveRecord queries run concurrently? I've also experienced this in production console.
I do have
config.threadsafe!
set inconfig/environment.rb
. If it matters, I'm using Rails 2.3.*These connections have to be manually closed? Production always has a lot of open connections that are doing nothing, causing
Mysql::Error: Too many connections
. I'll probably submit this issue as a another question.
原文:https://stackoverflow.com/questions/23689570
最满意答案
使用userTrackningMode可以使地图视图将地图置于该位置的中心,并开始跟踪用户的位置。 如果地图缩小,地图视图会自动放大用户的位置,从而有效地更改当前可见区域。
使用它
mapView.setUserTrackingMode(.follow, animated:true)
Use userTrackningMode which causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.
To use it
mapView.setUserTrackingMode(.follow, animated:true)
相关问答
更多-
MapView不会加载当前用户位置,也不会对其进行居中/缩放(MapView doesn't load Current User Location and doesn't center / zoom on it)[2023-06-29]
pragma mark - CLLocationManagerDelegate方法 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *newLocation = locations.lastObject; } - (void)locationManager: (CLLocationManager *)manager didFailWithEr ... -
使用.CentreToScreen() 喜欢这个: Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Me.Size = New System.Drawing.Size(400, 650) Me.CenterToScreen() End Sub Use .CentreToScreen() Like this: Private Sub Butt ...
-
我只是用这个编码更新到mapview中心位置 请将此密钥添加到info.plist文件中
NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription override func viewDidLoad() { super.viewDidLoad() // Do ... -
假设placemark实现了MKAnnotation协议,您可以将placemark的coordinate属性设置为mapView对象的centerCoordinate属性的值。 这意味着地标将始终放置在地图上,位于mapView当前所在位置的坐标处。 希望这可以帮助。 :) Assuming that placemark implements the MKAnnotation protocol, you can set the coordinate property of placemark to the ...
-
您可以通过根据屏幕和框架的大小计算位置来完成此操作。 static void setLocationToTopRight(JFrame frame) { GraphicsConfiguration config = frame.getGraphicsConfiguration(); Rectangle bounds = config.getBounds(); Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(con ...
-
使用userTrackningMode可以使地图视图将地图置于该位置的中心,并开始跟踪用户的位置。 如果地图缩小,地图视图会自动放大用户的位置,从而有效地更改当前可见区域。 使用它 mapView.setUserTrackingMode(.follow, animated:true) Use userTrackningMode which causes the map view to center the map on that location and begin tracking the user’s ...
-
这是一个使用jQuery的工作原型。 您需要查询窗口的滚动条位置( window.scrollY ),然后设置绝对定位的覆盖面板的顶部偏移量,并考虑窗口的高度。 之后,只需根据页面设计获得CSS样式。 $("body").click(function () { var pgt = window.scrollY; var vph = $(window).height(); var voff = pgt + vph / 2.0; $(".overlay").offset( ...
-
要缩放地图,您必须更改区域值意味着中心和span.once看到这一个Mapview缩放 在我的情况下,当我点击特定按钮时,我使用这个来移动地图。 MKCoordinateSpan span = MKCoordinateSpanMake(30.5982f,0.0001f); CLLocationCoordinate2D coordinate = {36, 90}; MKCoordinateRegion region = {coordinate, span}; M ...
-
您可以使用系统覆盖将视图放在所有屏幕的顶部.. 查看以下帖子了解更多信息: 创建系统覆盖窗口(始终在顶部) 创建主页按钮仍然有效的系统覆盖? AirCalc是另一个概念验证...... you can use system overlays to put a view on top of all the screens.. check out following post for more info: Creating a system overlay window (always on top) Crea ...
-
将CoreLocation.framework添加到项目中并添加到.h文件中 locationManager.delegate = self; // may be you forget to it. 并且还添加了委托方法,如, - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { ...