dlecocq / nsq-py

Hacking on Python NSQ Bindings
MIT License
44 stars 16 forks source link

there is an error when initializing a simple reader #44

Closed hicqu closed 8 years ago

hicqu commented 8 years ago

Hi, I tested a very simple reader with code below,

#!/usr/bin/env python2
# coding=utf-8
import datetime
from nsq.reader import Reader
from gevent.pool import Pool
from contextlib import closing

class SimpleReader:
    def __init__(self, pool_size, topic, channel, lookupd_http_addresses, func):
        self.deal = func
        self.pool = Pool(pool_size)
        self.reader = Reader(
            topic=topic, channel=channel,
            lookupd_http_addresses=lookupd_http_addresses,
            max_in_flight=100
            )

    def start(self):
        with closing(self.reader) as r:
            for message in r:
                self.deal(r)

def do_read(channel):
    channel = channel

    def consume(msg):
        n1 = datetime.datetime.now()
        msg.fin()
        n2 = datetime.datetime.now()
        print(n2 - n1)

    reader = SimpleReader(
        1, "test", channel,
        ["192.168.56.200:4161"],
        consume
        )
    reader.start()

if __name__ == "__main__":
    do_read("test")

but it shows error, ClientException: 'str' object is not callable

Could you help me? PS, I have already start my nsqlookupd server. On the other hand, I have also tryed to use nsqd_tcp_addresses instead of lookupd_http_addresses, and it' ok. So why can't I use only lookupd_http_addresses? Thanks a lot!

hicqu commented 8 years ago

Any one could help me? Thanks!!!!

dlecocq commented 8 years ago

I have a suspicion as to the problem. Can you do:

pip freeze | grep url
dlecocq commented 8 years ago

Specifically, we only recently pinned the version of url to <0.2.0 in setup.py, and using url==0.2.0 manifests as you've described. If you find that you have url==0.2.0:

pip install --upgrade url==0.1.7
hicqu commented 8 years ago

Yes, I use url 0.2.0! And Now I downgrade it to version 0.1.7 and use lookupd_http_addresses=[http://192.168.56.200:4161] as an arguement of Reader. It works! Thank you a lot! BTW, now both my develop environment and production environment use url 0.2.0, so I think supporting that is a good idea. What about you think?

dlecocq commented 8 years ago

Yeah, that's fine by me. It's actually probably a pretty minimal change. The main API difference is that utf8 is now a @property rather than a method, so the changes would have to happen here:

I'd welcome a PR if you're motivated :-)

krisdestruction commented 8 years ago

pip install --upgrade url==0.1.7 This fixed it for me, although it'd be nice with a PR for the issue.

kerrick-lyft commented 8 years ago

I've created a PR to fix this issue: https://github.com/dlecocq/nsq-py/pull/46