2014-09-30 59 views
1

由于Shopify的默认产品导入器(通过CSV)速度非常慢,我正在使用多线程技术为使用API​​的Shopify商店添加约24000种产品。该API有一个每秒2个的call limit。使用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 '=====================================================================================' 

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

回答