Skip to content

Instantly share code, notes, and snippets.

@smetj
Created February 28, 2018 14:21
Show Gist options
  • Save smetj/46908fd655e8e6a2b3928e6c16ea02d4 to your computer and use it in GitHub Desktop.
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
#!/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()
@smetj
Copy link
Author

smetj commented Feb 28, 2018

$ python queue_vs_channel.py 
Running test_Channel 
OK
Running test_Queue 
OK
3.6.4 (default, Jan 18 2018, 12:12:25) 
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]
Linux-4.14.18-200.fc26.x86_64-x86_64-with-fedora-26-Twenty_Six

    Compare Channels vs Queues
    
+--------------+--------------------+--------+---------+--------------+
| Function     | Description        | Result | Seconds | Iterations/s |
+--------------+--------------------+--------+---------+--------------+
| test_Channel | Pass using channel | OK     |  86.501 |        0.012 |
| test_Queue   | Pass using Queue   | OK     | 112.285 |        0.009 |
+--------------+--------------------+--------+---------+--------------+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment