Python 使用Django的非阻塞Thrift服务器
我需要通过接口访问来自Django webapp的数据。我希望以一种非阻塞的方式(例如,使用libevent/gevent…)来实现这一点,但是(在python中)没有太多的示例实现,所以我想在这里询问任何示例/提示/经验Python 使用Django的非阻塞Thrift服务器,python,django,nonblocking,thrift,thrift-protocol,Python,Django,Nonblocking,Thrift,Thrift Protocol,我需要通过接口访问来自Django webapp的数据。我希望以一种非阻塞的方式(例如,使用libevent/gevent…)来实现这一点,但是(在python中)没有太多的示例实现,所以我想在这里询问任何示例/提示/经验 请注意,这个问题是关于使用Thrift,而不是任何其他协议,我知道可能有比Django更好的框架用于此目的,但使用它也是一项要求 这可以通过在服务器中实现节约服务器来实现。有关所需的文件结构,请参见链接。在命令文件中,您可以将其称为“thrift_server.py”,然后按
请注意,这个问题是关于使用Thrift,而不是任何其他协议,我知道可能有比Django更好的框架用于此目的,但使用它也是一项要求 这可以通过在服务器中实现节约服务器来实现。有关所需的文件结构,请参见链接。在命令文件中,您可以将其称为“thrift_server.py”,然后按照如下方式实现常用的thrift server:
import sys
from django.core.management.base import BaseCommand
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
#import thrift files here
#now define the service handler according to your thrift method declaration
class ServiceHandler:
def __init__(self):
pass
#self.log = {}
def thriftMethodName(self, arg):
print "hello world!"
#here you have access to anything in the django framework
return True
class Command(BaseCommand):
def handle(self, *args, **kwargs):
handler = ServiceHandler()
processor = SaleService.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
# You could do one of these for a multithreaded server
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
self.stdout.write('Starting thrift server...')
server.serve()
self.stdout.write('done.')
(virtualenv) /django_project/ > python manage.py thrift_server
请注意,上面描述了多线程服务器选项,尽管我还没有测试这些选项
然后可以按如下方式运行守护程序:
import sys
from django.core.management.base import BaseCommand
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
#import thrift files here
#now define the service handler according to your thrift method declaration
class ServiceHandler:
def __init__(self):
pass
#self.log = {}
def thriftMethodName(self, arg):
print "hello world!"
#here you have access to anything in the django framework
return True
class Command(BaseCommand):
def handle(self, *args, **kwargs):
handler = ServiceHandler()
processor = SaleService.Processor(handler)
transport = TSocket.TServerSocket(port=9090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
# You could do one of these for a multithreaded server
#server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
#server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)
self.stdout.write('Starting thrift server...')
server.serve()
self.stdout.write('done.')
(virtualenv) /django_project/ > python manage.py thrift_server
在您的情况下,您可以扩展哪部分是服务器,哪部分是客户端吗?我已经在java和python之间完成了Thrift服务,它们分别充当服务器和客户端。但是我使用线程来实现这个目的。Thrift服务器应该在Django端。。。我还看到了一些使用线程的Python示例,但我对使用gevent或类似库的解决方案很好奇!