Python NS Simulator - multiple threads

def send_rest_install_req(records_queue, account, url_tab):

    ns = NotificationSimu(0, account,url_tab)

   ns.send_rest_install_profile_req(records_queue, account)

    pass

 

def _read_device_data():

    #Get out devices info. from txt file

    fd = open(DEVICES_DATA, 'r')

    devices  = fd.readlines()

    fd.close()

    return devices

    pass

 

def _init_url_tab():

    parser = ConfigParser.ConfigParser()

    parser.read(CFG_DATA)

    url_list = []

   url_list.append(parser.get("Notification Server","REST_InstallProfile"))

   url_list.append(parser.get("Notification Server","REST_InstallProfile2"))

   url_list.append(parser.get("Notification Server","REST_InstallProfile3"))

   url_list.append(parser.get("Notification Server","REST_InstallProfile4"))

    install_url_tab = tuple(url_list)

    return install_url_tab

    pass

 

def multiple_process_installprofile():

    #Read devices data

    devices = _read_device_data()

    #init url tab

    url_tab = _init_url_tab()

    #new settings

    records_max = 80000

    threads_max = 100

    step_multiple = 4

    accounts_idx = 0

    records_queue_list = []

    #so...

    records_per_thread = records_max /threads_max

    if not (records_per_thread %step_multiple == 0):

        print 'Warning:%d records are not match setp multiple %d!' % (records_per_thread,step_multiple)

        sys.exit(1)

    else:

        #set up queuesfor each thread, and it will hold devices' records

        for i inxrange(0, threads_max):

           queue = Queue.Queue(records_per_thread) #set max size

           start_pos = i * records_per_thread

           if start_pos > records_max:

               print 'Warning: Reach the max records!'

               break

           end_pos = start_pos + records_per_thread - 1

           if end_pos > records_max:

               end_pos = records_max

           print 'Thread %d: devices index: %d - %d!' % (i, start_pos,end_pos)

           for pos in xrange(start_pos, end_pos):

               queue.put(devices[pos])

           records_queue_list.append(queue)

 

    del devices[:]  #free memory

    starttime = time.time()

    stime = datetime.now()

    output  =  "mdmi RESTsimulator Start time : %s\n" %stime

    print output

    perf_log_content = output

    thread_list = []

    for i in xrange(0, threads_max):

        work_thread =threading.Thread(target=send_rest_install_req, args=(records_queue_list[i],accounts_idx, url_tab, ));

       work_thread.start()

       thread_list.append(work_thread)

        accounts_idx =(accounts_idx + 1) % 4

    for working_thread in thread_list:

       working_thread.join();

    endtime = time.time()

    etime = datetime.now()

    output = "mdmi REST simulator Endtime : %s\n" % etime

    print output

    perf_log_content = perf_log_content +output

    sec_cost = endtime - starttime

    output = "mdmi REST simulaotr sent%d request with time cost: %d seconds.\n" % (records_max, sec_cost)

    print output

    perf_log_content = perf_log_content +output

    output = '-'*40 + '\n'

    print output

    perf_log_content = perf_log_content +output

    save_perf_log(SIMU_LOG, perf_log_content)

    for queue in records_queue_list:

        while notqueue.empty():

           queue.get()

    pass

 

if __name__ == '__main__':

    multiple_process_installprofile()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值