Skip to content

Instantly share code, notes, and snippets.

@kaka19ace
Last active May 13, 2019 06:40
Show Gist options
  • Save kaka19ace/11a9c52d01792410c51b407b190b285a to your computer and use it in GitHub Desktop.
Save kaka19ace/11a9c52d01792410c51b407b190b285a to your computer and use it in GitHub Desktop.
test_gevent_idle.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
"""
Output Example:
start: num=0 st=1557729555989
start: num=1 st=1557729555989
start: num=2 st=1557729555989
start: num=3 st=1557729555989
start: num=4 st=1557729555989
idle_end: num=4 t=1557729555989
idle_end: num=3 t=1557729555989
idle_end: num=2 t=1557729555989
idle_end: num=1 t=1557729555989
idle_end: num=0 t=1557729555989
end: num=4, cost=101736
end: num=3, cost=101821
end: num=2, cost=101866
end: num=1, cost=101912
end: num=0, cost=101992
"""
from __future__ import print_function
import time
import gevent
def gen_test_func(num):
def func():
st = time.time()
print("start: num={} st={}".format(num, int(st*1000)))
gevent.idle()
print("idle_end: num={} t={}".format(num, int(time.time()*1000)))
gevent.sleep(0.1)
et = time.time()
print ("end: num={}, cost={}".format(num, int(et*1000000)-int(st*1000000)))
return func
def test_parallel():
tasks = []
for i in range(5):
tasks.append(
gevent.spawn(gen_test_func(i))
)
gevent.joinall(tasks, timeout=5)
print("")
def test_loop():
while True:
test_parallel()
time.sleep(5)
if __name__ == "__main__":
test_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment