test.py
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
import threading
import time
from cacheout import LRUCache
import sched
from __global import redis_cache, var
tasks = []
for i in range(10):
tasks.append(f'https://mp.weixin.qq.com/s/{i}')
cache = LRUCache()
users = []
def read_article(uuu, link, retry=False):
if cache.has(uuu):
w = 2 * 60
print('任务重投', uuu, threading.currentThread().getName())
sched.add_task(read_article, w, 'seconds', args=(uuu, link, True))
else:
print(int(time.time()), uuu, link, retry, threading.currentThread().getName())
var.redis.incr(link)
cache.set(uuu, 'wait', ttl=2 * 60)
# ----生成2w个号
for i in range(int(3e3)):
users.append('small_user:' + hex(i))
def genc_task(tt):
users_copy = users.copy() # 复制个新列表
random.shuffle(users_copy) # 打乱
stop = random.choice([360, 200, 400, 600])
needs = random.choice([1000, 1200, 1800, 2000])
var.redis.set(f'stops:{tt}', stop)
var.redis.set(f'needs:{tt}', needs)
print(tt, stop, needs)
n = 0
while n < needs:
second = random.randrange(1, stop)
try:
u = users_copy.pop()
count = var.redis.incr(u)
if count <= 8:
n += 1
sched.add_task(read_article, second+30, 'seconds', args=(u, tt,))
else:
print(u, '分配够了', count)
except IndexError:
print('不够了号')
break
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(30) as t:
for tt in tasks:
t.submit(genc_task, tt)
sched.start()
while True:
time.sleep(1)