-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_pymatrix.py
93 lines (63 loc) · 2.19 KB
/
async_pymatrix.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
import curses
import asyncio
import random
import sys
class playground(object):
def __init__(self, stdscr):
self.stdscr = stdscr
maxLine, maxColumn = stdscr.getmaxyx()
self.top = 0
self.left = 0
self.right = maxColumn - 1
self.bottom = maxLine - 1
curses.use_default_colors()
curses.curs_set(False)
random.seed()
def display_msg(self, msg):
self.stdscr.addstr(0, 0, str(msg))
self.stdscr.refresh()
async def matrix(self, target):
attr = curses.COLOR_GREEN
def gen_bin(length):
binary = []
for i in range(length):
binary.append(random.choice(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ&%$#@!~1234567890"))
while True:
yield from binary
position = []
while True:
if len(position) == 0:
position = [self.top]
length = random.randint(1, 15)
message = gen_bin(length)
self.stdscr.addstr(position[0], target, next(message), attr)
if position[0] < self.bottom:
position.insert(0, position[0] + 1)
self.stdscr.addstr(position[0], target, next(message), attr)
if (len(position) > length or position[0] == self.bottom) and len(position) > 0:
self.stdscr.addstr(position.pop(), target, " ")
self.stdscr.refresh()
await asyncio.sleep(0.05)
async def supervisor(self):
tasks = []
th = self.right - self.left
self.display_msg("current coroutines: %d" % th)
for i in range(self.left, self.right):
tasks.append(asyncio.ensure_future(self.matrix(i)))
try:
await asyncio.gather(*tasks)
except:
for i in tasks:
i.cancel()
def main(stdscr):
te = playground(stdscr)
loop = asyncio.get_event_loop()
supervisor = asyncio.ensure_future(te.supervisor())
try:
loop.run_until_complete(supervisor)
except KeyboardInterrupt:
supervisor.cancel()
if __name__ == "__main__":
curses.wrapper(main)