gpt4 book ai didi

python - 非阻塞扭曲 xmlrpc

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:53 28 4
gpt4 key购买 nike

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

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

最佳答案

听起来您可能想看看 txmongo .

关于python - 非阻塞扭曲 xmlrpc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22401592/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com