简体   繁体   English

非阻塞扭曲的xmlrpc

[英]Non blocking Twisted xmlrpc

I want create xmlrpc with twisted in non blocking, but I don't know. 我想用非阻塞方式创建xmlrpc,但我不知道。 I want call method in xmlrpc and I execute my method every time and from any client without wait. 我想要xmlrpc中的call方法,并且每次都从任何客户端执行我的方法,而无需等待。 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()

听起来您可能想看看txmongo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM