Created
March 15, 2018 07:26
-
-
Save yohm/bc0583489521fd9d780f7afe8d5fbd0f to your computer and use it in GitHub Desktop.
fiber_sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fibers | |
import time | |
root_fiber = None | |
sub_fibers = [] | |
t = 0.1 | |
def async(func, *args, **kwargs): | |
global root_fiber, sub_fibers | |
def _f(): | |
func(*args, **kwargs) | |
root_fiber.switch() | |
fb = fibers.Fiber(target=_f) | |
sub_fibers.append(fb) | |
def await(): | |
global root_fiber, sub_fibers | |
fb = fibers.Fiber.current() | |
sub_fibers.append(fb) | |
root_fiber.switch() | |
def enter(): | |
global root_fiber, sub_fibers | |
def _loop(): | |
while sub_fibers: | |
fb = sub_fibers.pop(0) | |
fb.switch() | |
root_fiber = fibers.Fiber(target=_loop) | |
def exit(): | |
global root_fiber, sub_fibers | |
if root_fiber.is_alive(): | |
root_fiber.switch() | |
# test | |
def my_func(i): | |
print("f phase1 %d" % i) | |
time.sleep(t) | |
await() | |
print("f phase2 %d" % i) | |
time.sleep(t) | |
await() | |
def f2(): | |
print("f2 phase1 %d" % i) | |
time.sleep(t) | |
await() | |
print("f2 phase2 %d" % i) | |
time.sleep(t) | |
async(f2) # nested async | |
print("f phase 3 %d" % i) | |
time.sleep(t) | |
def test_case1(): | |
enter() | |
for i in range(2): | |
async(my_func,i) | |
exit() | |
def test_case2(): | |
enter() | |
my_func(0) | |
for i in range(2): | |
async(my_func,i+1) | |
exit() | |
test_case2() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread, Condition | |
from queue import Queue | |
import time | |
q = Queue() | |
cvs = [] | |
threads = [] | |
t = 0.1 | |
def async(func, *args, **kwargs): | |
def _f(): | |
func(*args, **kwargs) | |
q.put(0) | |
t = Thread(target=_f) | |
threads.append(t) | |
def await(): | |
q_local = Queue() | |
cvs.append(q_local) | |
q.put(0) | |
q_local.get() | |
def loop(): | |
def _launch_all_threads(): | |
while threads: | |
t = threads.pop(0) | |
t.start() | |
q.get() | |
_launch_all_threads() | |
while cvs: | |
print(" in main thread") | |
q_local = cvs.pop(0) | |
q_local.put(0) | |
q.get() | |
_launch_all_threads() | |
# test | |
def my_func(i): | |
print("f phase1 %d" % i) | |
time.sleep(t) | |
await() | |
print("f phase2 %d" % i) | |
time.sleep(t) | |
await() | |
def f2(): | |
print("f2 phase1 %d" % i) | |
time.sleep(t) | |
await() | |
print("f2 phase2 %d" % i) | |
time.sleep(t) | |
async(f2) # nested async | |
print("f phase 3 %d" % i) | |
time.sleep(t) | |
for i in range(2): | |
async(my_func,i) | |
loop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fibers | |
def f1(): | |
print("in f1") | |
fb = fibers.Fiber(target=lambda:print("in nested fiber")) | |
print("done nested fiber") | |
fb = fibers.Fiber(target=f1) | |
fb.switch() | |
print("done in main fiber") | |
if fb.is_alive(): | |
fb.switch() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment