Skip to content

Commit f4e78e2

Browse files
author
minecraft server
committed
Forgot to add
1 parent 86c1006 commit f4e78e2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

plugins/long_operations.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
AUTHOR = "ekimekim"
3+
CONTACT = "[email protected]"
4+
DESCRIPTION = """This plugin allows for a function that takes a long time
5+
to do so in a way that will not cause a massive server lag.
6+
It does so with cooperative yields back to the main proxy.
7+
8+
Usage:
9+
@long_operation
10+
def my_big_func():
11+
yield
12+
i = 0
13+
for x in many_things:
14+
do_something(x)
15+
i += 1
16+
if i >= 1000:
17+
i = 0
18+
yield
19+
20+
This function will do_something with x 1000 times before allowing the proxy to continue.
21+
It will do so every tick until complete.
22+
23+
The return value (as it appears to the outside function) is the first value yielded.
24+
It is good practice to (as above) yield the return value early on, even if not distinct from later yields.
25+
This is especially important as the function must always yield at least once.
26+
Later yield values are ignored.
27+
"""
28+
29+
30+
def on_start():
31+
global running
32+
running = []
33+
34+
35+
def on_tick(users):
36+
global running
37+
for fn in running[:]:
38+
try:
39+
next(fn)
40+
except (Exception, StopIteration):
41+
running.remove(fn)
42+
43+
44+
def long_operation(fn):
45+
def _long_operation(*args, **kwargs):
46+
global running
47+
gen = fn(*args, **kwargs)
48+
running.append(gen)
49+
return next(gen)
50+
return _long_operation

0 commit comments

Comments
 (0)