1

I want create xmlrpc with twisted in non blocking, but I don't know. I want call method in xmlrpc and I execute my method every time and from any client without wait. this is my code:

from pymongo import MongoClient, ASCENDING, Connection, DESCENDING
from datetime import datetime, timedelta

from twisted.web import xmlrpc, server
import csv
import time

class MongoTest(xmlrpc.XMLRPC):
    allowNone = True
    useDateTime = True

    def __init__(self):
        xmlrpc.XMLRPC.__init__(self)
        self.dir = '/home/pythonu/Desktop/check.csv'
        self.dir_json = '/home/pythonu/Desktop/check.json'
        self.Dict = {}

    def GetTime(self, secs):
        """
            this is convert function from secs to strftime format
            pass your secs to this func for converting to strftime("%H:%M:%S")
        """

        c = ":"
        sec = timedelta(seconds=int(secs))
        d = datetime(1,1,1) + sec
        val = "%s:%s:%s" % (d.hour, d.minute, d.second)

    def xmlrpc_BulkToMongo(self, name_db, name_col, number):
        """
            added records by bulking insert
        """
        self.start = time.time()
        client = MongoClient()
        db = client[str(name_db)]
        db_col = db[str(name_col)]
        list_bulk = []

        with open(self.dir) as f:
            Dict = csv.DictReader(f)
            i = 0
            for doc in Dict:
                i += 1
                list_bulk.append(doc)
                if float(i % int(number)) == 0:
                    db.db_col.insert(list_bulk)
                    list_bulk = []
                    now = time.time() - self.start
                    now_time = self.GetTime(now)
                    print "\r%s records added by Bulking in my db after %s time\n " % (i,now_time)
        now = time.time() - self.start
        now_time = self.GetTime(now)
        print "\r%s records added by Bulking in my db after %s time\n " % (i,now_time)
        return "bulking %s records in %s time" % (i, now_time)

if __name__ == "__main__":
    from twisted.internet import reactor
    r = MongoTest()
    reactor.listenTCP(7081, server.Site(r))
    reactor.run()

how to non blocking this code such as below:

from twisted.web import xmlrpc, server
from twisted.internet import reactor
from twisted.internet.threads import deferToThread
from twisted.python import log
from twisted.internet.defer import Deferred

class Example(xmlrpc.XMLRPC):
    """An example object to be published."""

    def xmlrpc_echo(self, x):
        """Return all passed args."""
        return x

    def xmlrpc_block(self, duration=10):
        """block the instance for a specified duration"""   
        print "start"
        import time
        time.sleep(duration)
        return "i slept %s seconds!" % (str(duration))


    def xmlrpc_block2(self, duration=10):
        """block the instance for a specified duration"""
        print "start2"
        import time
        d = deferToThread(time.sleep, duration)
        d.addCallback(lambda r: "i slept %d seconds!" % duration)
        return d

    def xmlrpc_block3(self, duration=10):
        """block the instance for a specified duration"""
        import time
        d = Deferred()
        reactor.callLater(duration, d.callback, "i slept %d seconds!" % duration)
        return d
# this only runs if the module was *not* imported
if __name__ == '__main__':
    r = Example()
    reactor.listenTCP(7080, server.Site(r))
    reactor.run()
user255327
  • 47
  • 1
  • 6

1 Answers1

1

It sounds like you might want to look at txmongo.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
  • 1
    near, But I want to non-blocking my xmlrpc_BulkToMongo method. when i call from client this method, near of 3 minutes take to finish 4 million records insert into db,but until finished this method I don't call this method from other client.how to solve my issue? – user255327 Mar 14 '14 at 14:18
  • I want to write driver for mongodb in webservice with twisted. I put the project in githup. please checked it this [link](https://github.com/ali-hallaji/mongo-driver-web-service/blob/master/MongoDriver.py) – user255327 Mar 14 '14 at 14:28
  • I find my the problem. I use of deferToThread for async my method. – user255327 Mar 15 '14 at 08:41