对象是否存在本机类型安全包装和拆箱?(Is there native type-safe wrapping and unboxing for Object?)
我的查询是关于包装,使用Object类作为我的包装器。
我的目标是获取原始int(或int的包装)并从通用Object中取消它。
唯一的问题是我将在运行时使用混合类型,因此我无法一般地键入方法的返回类型。 T var1!=(T)var2
我现在可以做的是有过多的开销,因为我不能假设Object包含int类型的值,所以我解析它。
Object int_wrapped_in_object = 1; Integer.valueOf(String.valueOf(int_wrapped_in_object)); //I could cast from Object to int with: //(int) int_wrapped_in_object //but this will always anticipate a classcastexception in case the value was wrapped with another type, like String
这是有效的,但我最好跳过解析步骤,只需打开整数值框。
整数包装类不支持valueOf(Object o) ,可能是因为Object没有实现相对强制转换。 它(Object)支持toString() 。 但不是toInt()。 它非常奇特,因为Object确实可以包装一个原始int。 为什么会出现这种情况超出我的知识水平,所以我只能使用我的工作。 据我所知,甚至可能有原生支持将Object作为int展开。
请帮助我找到一个解决方案,该解决方案涉及使用最少的解析并引入尽可能少的异常来获取原始int值。
My inquiry is on wrapping, using the Object class as my wrapper.
My goal is to take the primitive int (or a wrapper of int) and unbox it from a generic Object.
The only problem is that I will be working with mixed types at runtime, so I am unable to generically type the return type of the method. T var1 != (T) var2
What I can do right now has excessive overhead because I cannot assume the Object wraps a value of type int, so I parse it.
Object int_wrapped_in_object = 1; Integer.valueOf(String.valueOf(int_wrapped_in_object)); //I could cast from Object to int with: //(int) int_wrapped_in_object //but this will always anticipate a classcastexception in case the value was wrapped with another type, like String
This works, but I would ideally like to skip a step of parsing and just unwrap box for the integer value.
Integer wrapper class does not support valueOf(Object o), likely because Object does not implement the relative cast. It (Object) supports toString(). But not toInt(). It is very peculiar because Object indeed can wrap a primitive int. Why that is the case is above my level of knowledge, so I can only work with what I have. For all I know there may even be native support for unwrapping an Object as an int.
Please help me find a solution that involves using minimal parsing and introducing as few exceptions to get the primitive int value.
原文:https://stackoverflow.com/questions/45699699
最满意答案
我认为你并没有从正确的方向接近这一点,因为你把你的应用程序作为myanimelist.net的代理太多了。
当你有2000个用户时会发生什么? 您的应用程序最终会对myanimelist.net发出大量请求,并且一个不太重要的用户可能会拒绝您的应用程序(或将其用于DoS myanimelist.net)。
这是一个更清洁的方式恕我直言:
服务器端 :
- 创建一个websocket服务器(例如: https : //github.com/aaugustin/websockets/blob/master/example/server.py )
- 当用户连接到websocket服务器时,将客户端添加到列表中,在断开连接时将其从列表中删除。
- 对于每个连接用户,请经常检查myanimelist.net以获取关联的xml(可能会降低更多在线用户的频率)
- 对于每个xml文档,使用您的服务器本地版本进行差异化,并使用websocket通道将该差异发送到客户端(假设有差异)。
- 客户端 :
- 在收到差异:更新本地XML与差异。
- 在停用n秒之后断开与websocket的连接+断开连接时在接口上添加一个按钮以重新连接
我怀疑你可以做更好的事情,假设myanimelist.net不提供“推送”API。
I think you aren't approaching this from the right direction because you place your app too much as a proxy of myanimelist.net.
What happens when you have 2000 users? Your app end up doing tons of requests to myanimelist.net, and a mean user could definitely DoS your app (or use it to DoS myanimelist.net).
This is a much cleaner way IMHO :
Server side :
- Create a websocket server (ex: https://github.com/aaugustin/websockets/blob/master/example/server.py)
- When a user connects to the websocket server, add the client to a list, remove it from the list on disconnect.
- For every connected users, do frequently check myanimelist.net to get the associated xml (maybe lower the frequence the more online users you get)
- for every xml document, make a diff with your server local version, and send that diff to the client using the websocket channel (assuming there is a diff).
- Client side :
- on receiving diff : update the local xml with the differences.
- disconnect from websocket after n seconds of inactivity + when disconnected add a button on the interface to reconnect
I doubt you can do anything much better assuming myanimelist.net doesn't provide a "push" API.
相关问答
更多-
我终于去了Flask-JWT( https://pypi.python.org/pypi/Flask-JWT/0.1.0 ) 这是我修改的最小例子: from flask import Flask, render_template, request, url_for, redirect from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.security import Security, SQLAlchemyUserDatastore, \ ...
-
烧瓶破碎管与请求(Flask broken pipe with requests)[2022-08-13]
在适当的WSGI服务器上运行您的烧瓶应用程序,能够处理并发请求(也许是爆米花灭火器或uWSGI ),它将会工作。 在开发中,在Flask提供的服务器中启用线程: app.run(threaded=True) 但请注意,Flask服务器不推荐用于生产使用。 发生什么事情是,使用请求您向烧瓶应用程序发出第二个请求,但是由于它仍然处于第一个处理状态,所以在第一个请求完成之前,它不会响应第二个请求。 顺便说一下,在Python 3下,插件服务器实现更加优雅地处理断开连接,并继续服务而不是崩溃。 Run your ... -
我可以通过Flask请求持久化一个http连接(或其他数据)吗?(Can I persist an http connection (or other data) across Flask requests?)[2022-10-04]
我认为你并没有从正确的方向接近这一点,因为你把你的应用程序作为myanimelist.net的代理太多了。 当你有2000个用户时会发生什么? 您的应用程序最终会对myanimelist.net发出大量请求,并且一个不太重要的用户可能会拒绝您的应用程序(或将其用于DoS myanimelist.net)。 这是一个更清洁的方式恕我直言: 服务器端 : 创建一个websocket服务器(例如: https : //github.com/aaugustin/websockets/blob/master/exam ... -
我想要做到这一点的最好方法是使用Wireshark,以及它的“Follow TCP Stream”功能。 这将使我在协议级别转储整个http事务。 The best way I've figure out to do this is use Wireshark, and its "Follow TCP Stream" feature. This will give me a dump of an entire http transaction, at the protocol level.
-
我找到了答案。 发布信息以帮助决定在IIS上实施Flask的人员,这可能会遇到同样的问题。 权限错误确实是IIS方面的一个问题。 您必须更改IIS的权限设置。 这个链接帮助我: 拒绝IIS7权限 - ASP文件写入 在IIS管理器中,我单击了连接窗格中的应用程序池 。 然后改为选择我的Flask应用程序(不是链接中所述的DefaultAppPool)。 我右键单击它并选择高级设置 。 然后我将Process Model部分下的Identity字段更改为LocalSystem 。 点击确定。 在这个改变之后, ...
-
您可以使用信号量: import threading import time sem = threading.Semaphore() @app.route("/",methods=['POST']) def main_process(): sem.acquire() # heavy process here to run alone sem.release() return "Done" 信号量用法是控制对公共资源的访问。 您可以在此处查看有关信号量的更多信息 这个问题也 ...
-
在内存中使用sqlite db的问题是无法从多个线程访问您的Sqlite内存数据库。 http://www.sqlite.org/inmemorydb.html 为了解决这个问题,您可能会有多个进程运行您的应用程序,这使得使用内存中的全局变量也是不可能的。 因此,除非您确定您的应用程序只需要单个线程或单个进程(这不太可能),否则您将需要: 使用磁盘存储状态,例如磁盘上的sqlite db,或者甚至只是解析的某个文件。 使用与应用程序分开运行的守护进程来管理状态。 我个人选择2。 您可以使用memcached ...
-
(发表评论作为答案) 您可以简单地使用r.text从文档中获取requests.post的retun数据: 当您发出请求时,Requests会根据HTTP标头对响应的编码进行有根据的猜测。 当您访问r.text时,将使用由Requests猜测的文本编码。 所以只需更换 return "Create new user sucesso!" 同 return r.text 在这种情况下, r.text是requests.post(url, data=json.dumps(payload), headers=h ...
-
在其他答案的基础上,并使用请求工具带库,代码将如下所示: from flask import Flask, Response from requests_toolbelt import MultipartEncoder app = Flask(__name__) @app.route('/downloads') def downloads(): m = MultipartEncoder( fields={'field0': 'value', 'field1': 'value ...
-
以下内容适用于您不希望为每个请求重新创建的任何全局Python数据,而不仅仅是rserve,而不仅仅是每个用户唯一的数据。 我们需要一些公共位置来为每个用户创建一个rserve连接。 最简单的方法是将multiprocessing.Manager作为单独的进程运行。 import atexit from multiprocessing import Lock from multiprocessing.managers import BaseManager import pyRserve connectio ...