twisted / txmongo

asynchronous python driver for mongo
https://txmongo.readthedocs.io
Apache License 2.0
338 stars 102 forks source link

txmongo/examples/deferreds/insert.py is not working in Python 3.6.x and mongo 3.2.16 #215

Closed harshathulasi closed 7 years ago

harshathulasi commented 7 years ago

When trying to execute the "txmongo/examples/deferreds/insert.py" script, script fails with the following message:

~/twsited_mongo_play$ python3 dummy.py 2017-09-10 18:53:36-0700 [-] Log opened. 2017-09-10 18:53:36-0700 [-] getting connection... 2017-09-10 18:53:36-0700 [-] Starting factory <txmongo.connection._Connection object at 0x7f04044ee160> 2017-09-10 18:53:36-0700 [-] Connection is: Connection('127.0.0.1', 27017) 2017-09-10 18:53:36-0700 [-] Traceback (most recent call last): 2017-09-10 18:53:36-0700 [-] File "dummy.py", line 76, in 2017-09-10 18:53:36-0700 [-] example() 2017-09-10 18:53:36-0700 [-] File "dummy.py", line 61, in example 2017-09-10 18:53:36-0700 [-] d.addCallback(getDatabase, "vel_test") 2017-09-10 18:53:36-0700 [-] TypeError: call() takes 2 positional arguments but 3 were given ~/twsited_mongo_play$

Not sure if this code is compatible with python3 or some version mismatch:

def example():
    d = getConnection()
    errback_out = d.addErrback(log.err)
    d.addCallback(getDatabase, "vel_test")
    d.addCallback(getCollection, "messages")
    d.addCallback(insertData)
IlyaSkriblovsky commented 7 years ago

Sorry, all examples in examples/deferreds/ seem to be broken :(

examples/deferreds/insert.py defines getConnection() to return MongoConnectionPool(), but then uses it like it is Deferred:

def getConnection():
    print "getting connection..."
    return txmongo.MongoConnectionPool()

. . .

def example():
    d = getConnection()
    d.addErrback(log.err)
    d.addCallback(getDatabase, "foo")

It can be fixed for example by replacing return txmongo.MongoConnectionPool() with return defer.succeed(txmongo.MongoConnectionPool()).

I will file a new issue about this with more specific title.

Actually, this particular example is bad anyway, please don't follow it :( It overuses deferreds for naturally synchronous tasks only to confuse things. deferreds/insert-consice.py is a bit better, but suffers from same problems too.

Please look at inlinecallbacks examples, they are much better (while not without problems, though).

IlyaSkriblovsky commented 7 years ago

Closing this in favor of #216