多线程间通信
import pymysql
import pandas as pd
import numpy as np
import csv
import time
import time
import datetime
import math
import psutil
import warnings
import threading
import queue
import random
import psutil
warnings.filterwarnings("ignore")
class customer_info:
def __init__(self,date1,date2):
#更新客户活跃程度 字段:'统计日期', '平台', '店铺', '买家昵称', '最近一次下单时间', '订单状态', '累计实付金额', '累计购买次数', '购买次数','客户活跃程度
def customer_active_level(self):
#更新活跃客户价值标签
def update_customer_label(self):
#全部客户资产统计
def customer_stat(self):
#活跃客户资产统计
def act_customer_stat(self):
class Producer(threading.Thread):
'''更新客户活跃程度测试'''
def __init__(self,date,q):
super(Producer,self).__init__()
self.q = q
self.date = date
def run(self):
self.q.put(date) #将通信信息加入队列
print('生产者运行日期是:%s' % date)
#self.update_customer_active_level(date)
self.q.task_done() #完成后向队列发出完成信号
def update_customer_active_level(self,date):
date2 = pd.date_range(end = date,periods = 2,freq = 'MS')[0].strftime('%Y-%m-%d')
customer_label = customer_info(date1, date2)
# 更新客户活跃程度
customer_label.customer_active_level()
class Customer(threading.Thread):
'''更新客户活跃程度测试'''
def __init__(self,date):
super(Customer,self).__init__()
self.date = date
def run(self):
pass
while flag:
#防止系统内存不足
mem = psutil.virtual_memory()
if mem.used/mem.total < 0.8:
print('消费者运行日期是:%s' % self.date)
#self.update_customer_info(self.date)
flag = False
else:
print('当前内存不足,请稍后')
def update_customer_info(self):
date2 = pd.date_range(end = self.date,periods = 2,freq = 'MS')[0].strftime('%Y-%m-%d')
customer_label = customer_info(self.date, date2)
flag = True
# 更新活跃客户价值标签
customer_label.update_customer_label()
# 更新客户资产信息表
customer_label.customer_stat()
customer_label.act_customer_stat()
if __name__ == '__main__':
q = queue.Queue(2)
time1 = time.time()
date_list = pd.date_range('2018-11-01','2020-01-01',freq = 'MS')
for date in date_list:
date = date.strftime('%Y-%m-%d %H:%m:%S')[:10]
producer = Producer(date,q)
producer.start()
q.join()
customer = Customer(date)
customer.start()
time2 = time.time()
print(time2 - time1)
目的是想让生产者一直运行,消费者在同期的生产者运行完成以后再运行。
posted on 2020-04-28 17:08 Learning_logs 阅读(144) 评论(0) 收藏 举报
浙公网安备 33010602011771号