-
Notifications
You must be signed in to change notification settings - Fork 1.4k
conn.py performance improvements, make examples work, add another example #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee7e86e
Update example.py to compile, add friendly load_example.py
9732ed1
Minor refactor in conn.py, update version in __init__.py, add ErrorSt…
25e63cc
Update logging config to include timestamps for better/easier debugging
3b39d9d
Fix grammar in error string
112158f
Merge branch 'master' into conn_refactor
888f206
Update load_example
a6fc260
Merge branch 'master' into conn_refactor
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
import logging | ||
#!/usr/bin/env python | ||
import threading, logging, time | ||
|
||
from kafka.client import KafkaClient, FetchRequest, ProduceRequest | ||
from kafka.client import KafkaClient | ||
from kafka.consumer import SimpleConsumer | ||
from kafka.producer import SimpleProducer | ||
|
||
def produce_example(client): | ||
producer = SimpleProducer(client, "my-topic") | ||
producer.send_messages("test") | ||
class Producer(threading.Thread): | ||
daemon = True | ||
|
||
def consume_example(client): | ||
consumer = SimpleConsumer(client, "test-group", "my-topic") | ||
for message in consumer: | ||
print(message) | ||
def run(self): | ||
client = KafkaClient("localhost:9092") | ||
producer = SimpleProducer(client) | ||
|
||
while True: | ||
producer.send_messages('my-topic', "test") | ||
producer.send_messages('my-topic', "\xc2Hola, mundo!") | ||
|
||
time.sleep(1) | ||
|
||
|
||
class Consumer(threading.Thread): | ||
daemon = True | ||
|
||
def run(self): | ||
client = KafkaClient("localhost:9092") | ||
consumer = SimpleConsumer(client, "test-group", "my-topic") | ||
|
||
for message in consumer: | ||
print(message) | ||
|
||
def main(): | ||
client = KafkaClient("localhost:9092") | ||
produce_example(client) | ||
consume_example(client) | ||
threads = [ | ||
Producer(), | ||
Consumer() | ||
] | ||
|
||
for t in threads: | ||
t.start() | ||
|
||
time.sleep(5) | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig(level=logging.DEBUG) | ||
logging.basicConfig( | ||
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s', | ||
level=logging.DEBUG | ||
) | ||
main() |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python | ||
import threading, logging, time, collections | ||
|
||
from kafka.client import KafkaClient | ||
from kafka.consumer import SimpleConsumer | ||
from kafka.producer import SimpleProducer | ||
|
||
msg_size = 524288 | ||
|
||
class Producer(threading.Thread): | ||
daemon = True | ||
big_msg = "1" * msg_size | ||
|
||
def run(self): | ||
client = KafkaClient("localhost:9092") | ||
producer = SimpleProducer(client) | ||
self.sent = 0 | ||
|
||
while True: | ||
producer.send_messages('my-topic', self.big_msg) | ||
self.sent += 1 | ||
|
||
|
||
class Consumer(threading.Thread): | ||
daemon = True | ||
|
||
def run(self): | ||
client = KafkaClient("localhost:9092") | ||
consumer = SimpleConsumer(client, "test-group", "my-topic", | ||
max_buffer_size = None, | ||
) | ||
self.valid = 0 | ||
self.invalid = 0 | ||
|
||
for message in consumer: | ||
if len(message.message.value) == msg_size: | ||
self.valid += 1 | ||
else: | ||
self.invalid += 1 | ||
|
||
def main(): | ||
threads = [ | ||
Producer(), | ||
Consumer() | ||
] | ||
|
||
for t in threads: | ||
t.start() | ||
|
||
time.sleep(10) | ||
print 'Messages sent: %d' % threads[0].sent | ||
print 'Messages recvd: %d' % threads[1].valid | ||
print 'Messages invalid: %d' % threads[1].invalid | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig( | ||
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s', | ||
level=logging.DEBUG | ||
) | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to set
_socket
here since it's set inreinit()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self._sock is required because the first line of reinit() asks "if not self._sock"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you mean in
close()
. You're right