Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Ruby on Rails将多个数据插入数据库_Ruby On Rails_Ruby_Database_Multithreading - Fatal编程技术网

Ruby on rails 使用Ruby on Rails将多个数据插入数据库

Ruby on rails 使用Ruby on Rails将多个数据插入数据库,ruby-on-rails,ruby,database,multithreading,Ruby On Rails,Ruby,Database,Multithreading,我想这是我在这里的第一个问题,希望尽可能清楚。我有休闲代码 def index_service_name @currentService = Feed.where("name = '#{params[:name]}'").first serviceId = @currentService['id'] serviceName = @currentService['name'] serviceFeedUrl = @currentService['feedUrl'] feed = Feedzirr

我想这是我在这里的第一个问题,希望尽可能清楚。我有休闲代码

def index_service_name
@currentService = Feed.where("name = '#{params[:name]}'").first

serviceId = @currentService['id']
serviceName = @currentService['name']
serviceFeedUrl = @currentService['feedUrl']

feed = Feedzirra::Feed.fetch_and_parse(serviceFeedUrl)

  feed.entries.reverse.each do |entry|
    case serviceName
      when 'service1', 'service2'
        uniqueId = entry.url.match(/\d+$/)[0]
        postContent = Nokogiri::HTML( entry.content ).css('img').map{ |i| i['src'] }.first # this would be an array.
      else
        uniqueId = entry.url
        postContent = entry.content
    end

    isIndexed = Post.where("post_unique_id = '#{uniqueId}' AND post_service = '#{serviceId}'")

    if postContent =~ %r{\Ahttps?://.+\.(?:jpe?g|png|gif)\z}i
      isImage = true
    elsif postContent =~ %r{http?s://(.*)/maxW500/}i
      isImage = true
    end

    if isIndexed.empty? && isImage
      sleep 1.seconds
      Post.create(post_service: serviceId, post_service_name: serviceName, title: entry.title, content: postContent, url: entry.url, post_unique_id: uniqueId)
    end
  end
我使用常规URL(/something/service/service1、/something/service/service2)触发服务。如果我同时触发它们,似乎它们中的每一个都在等待另一个结束(因此,在我的数据库中,数据首先从service1存储,然后从service2存储)。我认为这与多线程有关,据我所知ROR还不支持多线程


我是ROR的新手,所以请温柔一点。非常感谢您的帮助。

假设您使用的是MRI ruby,那么您就没有并行多线程。MRI有绿色的线程,它们不会并行运行。如果您使用ruby的JRuby实现,您将能够使用并行多线程


无论哪种方式,启动服务器的两个实例,您将能够并行运行该代码。使用
rails s-p3000
rails s-p3001
启动两台服务器:端口3001和3000。然后尝试访问端口3000上的一个url和端口3001上的另一个url。这样,您的代码将并行运行

你是如何运行这个应用程序的?Unicorn,脚本/rails服务器,或者其他什么?基本rails服务器(控制台中的rails s)。好的,我明白了。所以基本上,如果我有10个服务,我需要启动我的应用程序的10个实例?听起来不太好。还有别的选择吗?改变什么?谢谢。这是为了发展。在生产中,您将拥有apache+passenger,它将自动启动进程并在同一端口上运行,并将请求路由到可用的processOk。我将尝试将标签切换到生产,看看发生了什么。我目前正在macbook上使用localhost。我也需要启动Apache吗?仅仅将标记切换到生产将没有帮助。实际上,您需要安装apache和passenger fusion。然后,您需要编辑apacheconf文件以指向项目的公共目录,并启用passenger,以便apache开始提供rails。如果你想这样做,请研究如何设置apache和passenger。嗯,好的。为了“仅仅测试”一个应用程序,需要做很多工作。也没有多线程。。。在不久的将来,我不认为ROR是一种可以使用的语言(至少在我的应用程序中是这样)。谢谢你的帮助,但我可能会转到Laravel(关于PHP)。