在Rails中进行线程处理以获取大量请求

时间:2013-01-04 06:30:19

标签: ruby-on-rails-3 multithreading process

我有以下代码

def test
  t1 = Time.now
  @lat = params[:lat]
  @lng = params[:lng]
  @vehicleid = params[:vehicle_id]
  @address = GoogleGeocoder.reverse_geocode([@lat, @lng]).full_address

  speed = params[:speed]
  speed ||= 0
  Tracking.create(:latitude => @lat, :longitude => @lng, :address => @address, :vehicle_id => @vehicleid, :speed => speed)


  uid = Vehicle.select('user_id').where('vehicle_id=?', @vehicleid)
  gid = VehicleGeozone.where('vehicle_id=?', @vehicleid)
  email = User.select('email').where('user_id=?', uid[0].user_id)
  gzone = Geozone.where('geozone_id=?', gid[0].geozone_id)

  @geofence = Geofence.where('geozone_id = ?', gzone[0].geozone_id)
  if gzone[0].zonetype == 'polygon'
    infence = false
    res = isPointInPoly(@lat.to_f, @lng.to_f, @geofence)
  else
    res = isPointInCircle(@lat.to_f, @lng.to_f, @geofence[0].latitude, @geofence[0].longitude, @geofence[0].radius)
  end

  if res == true
    AlertMailer.geofence(email[0].email).deliver
  end
  t2 = Time.now
  render :text => '{"message":"success"}'
end

此测试功能每5秒调用一次。

从t1和t2发现处理此功能需要7秒钟。

有没有办法使用线程,以便我可以将isPointInCircleisPointInPoly的密集代码移动到不同的线程中?

0 个答案:

没有答案