Created
February 28, 2018 14:21
-
-
Save smetj/46908fd655e8e6a2b3928e6c16ea02d4 to your computer and use it in GitHub Desktop.
A superficial test to see if there is any difference between Gevent Queue and Channel
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
#!/usr/bin/python | |
from testlap import TestLap | |
from gevent.queue import Queue, Channel | |
from gevent.pool import Pool | |
from gevent import sleep | |
class QueueVSChannel(): | |
''' | |
Compare Channels vs Queues | |
''' | |
def __init__(self, size=1000): | |
self.size = size | |
def test_Channel(self): | |
'''Pass using channel''' | |
def consume(c): | |
while True: | |
item = c.get() | |
# print("consume: %s" % item) | |
if item == "stop": | |
return | |
def produce(c): | |
for item in range(self.size): | |
# print("produce: %s" % item) | |
c.put(item) | |
c.put("stop") | |
c = Channel() | |
p = Pool() | |
p.spawn(produce, c) | |
p.spawn(consume, c) | |
p.join() | |
def test_Queue(self): | |
'''Pass using Queue''' | |
def consume(c): | |
while True: | |
item = c.get() | |
# print("consume: %s" % item) | |
sleep(0) # required otherwise we don't switch between greenlets | |
if item == "stop": | |
return | |
def produce(c): | |
for item in range(self.size): | |
# print("produce: %s" % item) | |
c.put(item) | |
sleep(0) # required otherwise we don't switch between greenlets | |
c.put("stop") | |
q = Queue() | |
p = Pool() | |
p.spawn(produce, q) | |
p.spawn(consume, q) | |
p.join() | |
if __name__ == '__main__': | |
instance = QueueVSChannel(10000000) | |
test_lap = TestLap(instance=instance, iterations=1) | |
test_lap.go() |
Author
smetj
commented
Feb 28, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment