22from statistics import Statistics
33from connect import connect
44from printer import Printer
5+ import threading
56import asyncio
67
78def guide_of_console ():
@@ -19,28 +20,27 @@ def guide_of_console():
1920 print (' ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄' )
2021
2122
22- async def preprocess_send_danmu_msg_andriod ():
23+ def preprocess_send_danmu_msg_andriod ():
2324 msg = input ('请输入要发送的信息:' )
2425 roomid = input ('请输入要发送的房间号:' )
25- await utils . send_danmu_msg_andriod ( msg , int (roomid ))
26+ Biliconsole (). append2list_console ([[ msg , int (roomid )], utils . send_danmu_msg_andriod ] )
2627
27- async def preprocess_send_danmu_msg_web ():
28+ def preprocess_send_danmu_msg_web ():
2829 msg = input ('请输入要发送的信息:' )
2930 roomid = input ('请输入要发送的房间号:' )
30- await utils . send_danmu_msg_web ( msg , int (roomid ))
31+ Biliconsole (). append2list_console ([[ msg , int (roomid )], utils . send_danmu_msg_web ] )
3132
32- async def preprocess_check_room ():
33+ def preprocess_check_room ():
3334 roomid = input ('请输入要转化的房间号:' )
34- await utils .check_room ( roomid )
35+ Biliconsole (). append2list_console ([[ roomid ], utils .check_room ] )
3536
3637def process_send_gift_web ():
37- utils . fetch_bag_list ( verbose = True )
38+ Biliconsole (). append2list_console ([[ True ], utils . fetch_bag_list ] )
3839 bagid = input ('请输入要发送的礼物编号:' )
39- giftid = utils .fetch_bag_list (bagid = bagid )
4040 # print('是谁', giftid)
4141 giftnum = input ('请输入要发送的礼物数目:' )
4242 roomid = input ('请输入要发送的房间号:' )
43- utils . send_gift_web ( roomid , giftid , giftnum , bagid )
43+ Biliconsole (). append2list_console ([[ roomid , [[ False , bagid ], utils . fetch_bag_list ], giftnum , bagid ], utils . send_gift_web ] )
4444
4545def preprocess_change_danmuji_roomid ():
4646 roomid = input ('请输入roomid' )
@@ -57,14 +57,14 @@ def change_printer_dic_user():
5757options = {
5858 '1' : Statistics ().getlist ,
5959 '2' : Statistics ().getresult ,
60- '3' : utils .fetch_bag_list ,
61- '4' : utils .fetch_medal ,
62- '5' : utils .fetch_user_info ,
63- '6' : utils .check_taskinfo ,
64- '7' : preprocess_send_danmu_msg_andriod ,
65- '8' : preprocess_send_danmu_msg_web ,
66- '9' : preprocess_check_room ,
67- '10' : process_send_gift_web ,
60+ '3' : utils .fetch_bag_list ,#async
61+ '4' : utils .fetch_medal ,#async
62+ '5' : utils .fetch_user_info ,#async
63+ '6' : utils .check_taskinfo ,#async
64+ '7' : preprocess_send_danmu_msg_andriod ,#input async
65+ '8' : preprocess_send_danmu_msg_web ,#input async
66+ '9' : preprocess_check_room ,#input async
67+ '10' : process_send_gift_web ,#input async !!!
6868 '11' : preprocess_change_danmuji_roomid ,
6969 '12' : change_printer_dic_user ,
7070 'help' : guide_of_console
@@ -73,25 +73,68 @@ def change_printer_dic_user():
7373def return_error ():
7474 print ('命令无法识别,请重新输入' )
7575
76- async def controler ():
76+ def controler ():
7777 while True :
7878 x = input ('' )
79- if x == '7' :
79+ # input and async
80+ if x == ['7' , '8' , '9' , '10' ]:
8081 # func = options.get(x, return_error)
81- await preprocess_check_room ()
82+ args , func = options .get (x , return_error )()
83+ #print(args)
84+ #Biliconsole().append2list_console(answer)
85+ # async
86+ elif x in ['3' , '4' , '5' , '6' ]:
87+ answer = options .get (x , return_error )
88+ Biliconsole ().append2list_console (answer )
89+ # normal
8290 else :
8391 options .get (x , return_error )()
84- await asyncio .sleep (0.1 )
8592
86- def main ():
87- loop = asyncio .new_event_loop ()
88- tasks = [
89- controler ()
90- ]
91-
92- loop .run_until_complete (asyncio .wait (tasks ))
93- #console_thread.join()
93+ class Biliconsole ():
94+ instance = None
95+
96+ def __new__ (cls , * args , ** kw ):
97+ if not cls .instance :
98+ cls .instance = super (Biliconsole , cls ).__new__ (cls , * args , ** kw )
99+ cls .instance .list_console = []
100+ cls .lock = threading .Lock ()
101+ return cls .instance
102+
103+ def append2list_console (self , request ):
104+ self .lock .acquire ()
105+ self .list_console .append (request )
106+ self .lock .release ()
107+
108+ async def run (self ):
109+ while True :
110+ len_list_console = len (self .list_console )
111+ tasklist = []
112+ for i in self .list_console :
113+ if isinstance (i , list ):
114+ # 对10号单独简陋处理
115+ for j in range (len (i [0 ])):
116+ if isinstance (i [0 ][j ], list ):
117+ i [0 ][j ] = await i [0 ][j ][1 ](* (i [0 ][j ][0 ]))
118+ task = asyncio .ensure_future (i [1 ](* i [0 ]))
119+ else :
120+ task = asyncio .ensure_future (i ())
121+ tasklist .append (task )
122+ if tasklist :
123+ await asyncio .wait (tasklist , return_when = asyncio .ALL_COMPLETED )
124+ #print('本批次结束')
125+ else :
126+ #print('本批次轮空')
127+ pass
128+ self .lock .acquire ()
129+ del self .list_console [:len_list_console ]
130+ self .lock .release ()
131+ if len_list_console == 0 :
132+ await asyncio .sleep (1 )
133+ else :
134+ await asyncio .sleep (0.3 )
135+
136+
137+
94138
95- loop .close ()
96139
97140
0 commit comments