多线程队列执行任务

#!/usr/bin/python

# coding:utf-8
# import redis

import redis
import sys
import threading
import time
import os

host = 'redis-host'
port = 6379
password = 'redis-pass'

#任务执行最大子线程数
cohort = 3 

try:
        redisconn = redis.Redis(host,port,0,password)
except Exception as e:
        print('Connect Error!')
        sys.exit(1)

def run(redisconn):

        while 1:
                val = redisconn.lpop('cacheKey')
                print(val)
                if val != None:
                        os.system('执行系统命令')
                else:
                        break
                pass

while 1:
        objlen = redisconn.llen('cacheKey')

        if(objlen == 0):
                time.sleep(1)
        if(objlen < 3):
                tmpcohort = objlen
        else:
                tmpcohort = cohort
        for i in range(tmpcohort):
                t = threading.Thread(target=run,args=(redisconn,))
                t.start()
                # t.join()
        pass




#  pip install redis-py-cluster
#  pip install redis 

赤狐博客 - 对于互联网行业的胡诌八扯
请先登录后发表评论
  • 最新评论
  • 总共0条评论