2014-09-30 55 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 '=====================================================================================' 

我可以嘗試解決這個問題?

回答