我正在使用实现WSGI请求和响应的Bottle框架,并且由于单线程问题,我将服务器更改为PythonWSGIServer并使用Apache bench进行测试但结果包含错误中断管道,这与此问题类似如何防止errno 32中断管?.我已经尝试了答案但无济于事.
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/paste/httpserver.py", line 1068, in process_request_in_thread
self.finish_request(request, client_address)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 323, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 641, in __init__
self.finish()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 694, in finish
self.wfile.flush()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 303, in flush
self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
Run Code Online (Sandbox Code Playgroud)
服务器代码如下所示,我不知道如何使用线程池改善连接?
from paste import httpserver
@route('/')
def index():
connection = pymongo.MongoClient(connectionString)
db = connection.test
collection = db.test
return str(collection.find_one())
application = default_app()
httpserver.serve(application, host='127.0.0.1', port=8082)
Run Code Online (Sandbox Code Playgroud) 我目前正在为搜索查询实现一个 Rest API。该方法采用多个过滤器参数,例如关键字、名称、标题、描述、日期等。为了保持 DRY 并避免重复 if else 子句,我正在寻找从数据库中检索资源的最佳实践方法。假设数据库公开了两个方法findByUsername(...)和findByDescription(),我们希望通过一个安静的 Web 界面公开它们。一个简单的实现将使用类似于下面的 if 和 else 子句。
@GET
@Path("users")
public User getUsers(@QueryParam("username") String username,
@QueryParam("description") String description) {
User user = null;
if (username != null) {
// execute findByUsername method to the database
user = findByUsername(username);
} else if (description != null) {
// execute findByDescription method to the database
user = findByDescription(description);
}
return user;
}
Run Code Online (Sandbox Code Playgroud)
问题是如何改进上面的代码以避免多余的 if else(检查)子句并保持 DRY?我正在使用 Jersey JaxRS 2.0 实现 Web 服务
我已经在堆栈溢出中读了很多问题,我想要的是删除小数后面的两个或两个以上的尾随零.即:
12.00 ==> 12
12.30 ==> 12.30
12.35 ==> 12.35
12.345678 ==> 12.34
Run Code Online (Sandbox Code Playgroud)