from __future__ import annotations """ productslist_search → ready_list2_copy1 搜索商品没有分类 allSortedSpuId,店铺信息从 food_table 拼;keyword 用 productslist_search.key_word """ import multiprocessing import time import newsetting # ==================== 运行参数(改这里) ==================== DB_HOST = '192.168.5.2' DB_PORT = 3307 DB_NAME = 'mt_newretail' workid = '20260904' BATCH_SIZE = 20 NUM_PROCESSES = 32 # ============================================================ process_tent = None def init_worker(): global process_tent process_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME) def get_data_batch(poi_ids): """按门店拉店铺信息 + 搜索商品,直接拼成 ready_list2_copy1 行""" poi_str = "','".join(poi_ids) sql = f""" SELECT t1.parent_id, t1.workid, t1.business_code, t1.mt_org_id, t1.poi_id_str, t1.shop_name, t1.address, t1.shipping_fee_tip, t1.min_price_tip, t1.packet_fee_tip, t1.shipping_time, NULL AS tag1, NULL AS tag1_name, NULL AS tag2, NULL AS tag2_name, NULL AS tag_type, NULL AS product_count, NULL AS allSortedSpuId, t2.product_id, t2.product_name, t2.description, t2.praise_content, t2.month_saled_content, t2.skus_id, t2.spec, t2.actual_price, t2.price, t2.origin_price, t2.activity_price, t2.min_order_count, t2.quota_per_order, t2.dynamic_text, t2.dynamic_url, t2.upccode, t2.want_to_buy_content, t2.praise_rate, t2.picture, t2.promotion_info, t2.tag, t2.activity_tag_id, t2.stock_label, t2.real_stock, NULL AS skus_count, t2.product_label, t2.vague_picture, t2.create_time, t2.key_word FROM ( SELECT * FROM food_table WHERE workid LIKE '%{workid}%' AND poi_id_str IN ('{poi_str}') ) AS t1 INNER JOIN ( SELECT * FROM productslist_search WHERE workid LIKE '%{workid}%' AND poi_id_str IN ('{poi_str}') ) AS t2 ON t1.poi_id_str = t2.poi_id_str AND t1.mt_org_id <=> t2.mt_org_id """ rows = process_tent.get_shoppingmalls(sql) return rows or [] def process_batch_task(poi_ids): try: rows = get_data_batch(poi_ids) print(f'[{time.strftime("%H:%M:%S")}] 批次完成: {len(poi_ids)} 门店, 匹配: {len(rows)} 条') return poi_ids, rows except Exception as e: print(f"批次处理出错: {e}") import traceback traceback.print_exc() return poi_ids, [] def main(): temp_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME) task = temp_tent.get_shoppingmalls( f"SELECT DISTINCT poi_id_str FROM productslist_search WHERE workid LIKE '%{workid}%'" ) all_poi_ids = [i[0] for i in task] print(f"productslist_search 共有 {len(all_poi_ids)} 个门店") poi_batches = [all_poi_ids[i:i + BATCH_SIZE] for i in range(0, len(all_poi_ids), BATCH_SIZE)] print(f"共分成 {len(poi_batches)} 个批次") final_tent = newsetting.All_Setting(DB_HOST, DB_PORT, DB_NAME) insert_sql = """ insert into ready_list2_copy1 ( parent_id, workid, business_code, mt_org_id, poi_id_str, shop_name, address, shipping_fee_tip, min_price_tip, packet_fee_tip, shipping_time, tag1, tag1_name, tag2, tag2_name, tag_type, product_count, allSortedSpuId, product_id, product_name, description, praise_content, month_saled_content, skus_id, spec, actual_price, price, origin_price, activity_price, min_order_count, quota_per_order, dynamic_text, dynamic_url, upccode, want_to_buy_content, praise_rate, picture, promotion_info, tag, activity_tag_id, stock_label, real_stock, skus_count, product_label, vague_picture, create_time, keyword ) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """ total_written = 0 with multiprocessing.Pool(processes=NUM_PROCESSES, initializer=init_worker) as pool: for handled_ids, res_list in pool.imap_unordered(process_batch_task, poi_batches): if res_list: try: final_tent.write_data(insert_sql, res_list) total_written += len(res_list) print(f'>>> 已写入数据库: {len(res_list)} 条 (累计: {total_written} 条)') del res_list except Exception as e: print(f"写入数据库失败: {e}") else: print(f"本批无数据: {handled_ids}") print(f"[{time.strftime('%H:%M:%S')}] 完成,共计 {total_written} 条数据。") if __name__ == '__main__': main()