为什么这些线程停止工作?

时间:2014-09-30 12:42:06

标签: ruby multithreading shopify

由于Shopify的默认产品导入程序(通过CSV)非常慢,我使用多线程使用API​​将~24000个产品添加到Shopify商店。 API的call limit为每秒2。有4个线程,调用在限制范围内。

但过了一会儿所有线程都停止工作,除了一个。我没有收到任何错误消息,代码仍在运行,但不会打印任何产品信息。我似乎无法弄清楚出了什么问题。

这是我正在使用的代码:

require 'shopify_api'
require 'open-uri'
require 'json'
require 'base64'

begin_time = Time.now
my_threads = []

shop_url  = "https://<API_KEY>:<PASSWORD>@<SHOPNAME>.myshopify.com/admin"

ShopifyAPI::Base.site = shop_url

raw_product_data = JSON.parse(open('<REDACTED>') {|f| f.read }.force_encoding('UTF-8'))

# Split raw product data
one, two, three, four = raw_product_data.each_slice( (raw_product_data.size/4.0).round ).to_a

def category_to_tag(input)
  <REDACTED>
end

def bazookah(array, number)
  array.each do |item|
    single_product_begin_time = Time.now

    # Store item data in variables
    vendor = item['brand'].nil? ? 'Overige' : item['brand']
    title = item['description']
    item_size = item['salesUnitSize']
    body = "#{vendor} - #{title} - #{item_size}"
    type = item['category'].nil? ? 'Overige' : item['category']
    tags = category_to_tag(item['category']) unless item['category'].nil?
    variant_sku = item['itemId']
    variant_price = item['basePrice']['price']

    if !item['images'].nil? && !item['images'][2].nil?
      image_src = item['images'][2]['url']
    end

    image_time_begin = Time.now
    image = Base64.encode64(open(image_src) { |io| io.read }) unless image_src.nil?

    image_time_end = Time.now
    total_image_time = image_time_end - image_time_begin

    # Create new product
    new_product = ShopifyAPI::Product.new
    new_product.title = title
    new_product.body_html = body
    new_product.product_type = type
    new_product.vendor = vendor
    new_product.tags = item['category'].nil? ? 'Overige' : tags

    new_product.variants = [ <REDACTED> ]

    new_product.images = [ <REDACTED> ]

    new_product.save

    creation_time = Time.now - single_product_begin_time

    puts "#{number}: #{variant_sku} - P: #{creation_time.round(2)} - I: #{image_src.nil? ? 'No image' : total_image_time.round(3)}"
  end
end


puts '====================================================================================='
puts "#{raw_product_data.size} products loaded. Starting import at #{begin_time}..."
puts '-------------------------------------------------------------------------------------'

my_threads << Thread.new { bazookah(one, 'one') }
my_threads << Thread.new { bazookah(two, 'two') }
my_threads << Thread.new { bazookah(three, 'three') }
my_threads << Thread.new { bazookah(four, 'four') }

my_threads.each { |thr| thr.join }

puts '-------------------------------------------------------------------------------------'
puts "Done. It took #{Time.now - begin_time} minutes."
puts '====================================================================================='

我可以尝试解决这个问题?

1 个答案:

答案 0 :(得分:1)

这很可能与此有关:

http://docs.shopify.com/api/introduction/api-call-limit

我怀疑您受到Shopify的限制。您正尝试通过API从单个IP地址添加24000条记录。大多数人不喜欢那种东西。