当前位置: 代码迷 >> python >> 非阻塞扭曲的xmlrpc
  详细解决方案

非阻塞扭曲的xmlrpc

热度:58   发布时间:2023-07-14 09:48:35.0

我想用非阻塞方式创建xmlrpc,但我不知道。 我想要xmlrpc中的call方法,并且每次都从任何客户端执行我的方法,而无需等待。 这是我的代码:

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()

如何不阻止此代码,如下所示:

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()

听起来您可能想看看 。