Python线程和原子操作(Python threads and atomic operations)
我想用同步
stop()
方法实现一个线程。我见过这样的版本:
class Thread1: def __init__(self): self._stop_event = threading.Event() self._thread = None def start(self): self._thread = threading.Thread(target=self._run) self._thread.start() def stop(self): self._stop_event.set() self._thread.join() def _run(self): while not self._stop_event.is_set(): self._work() def _work(self): print("working")
但我已经读过,原子操作是线程安全的,在我看来,它可以在没有
Event
情况下完成。 所以我想出了这个:class Thread2: def __init__(self): self._working = False self._thread = None def start(self): self._working = True self._thread = threading.Thread(target=self._run) self._thread.start() def stop(self): self._working = False self._thread.join() def _run(self): while self._working: self._work() def _work(self): print("working")
它认为类似的实现在C中被认为是不正确的,因为编译器可以将
_working
放到一个寄存器中(甚至可以优化),并且工作线程永远不会知道该变量已经改变。 像这样的事情发生在Python? 这个实现是否正确? 我不打算完全避免事件或锁,只是想了解这个原子操作的事情。I want to implement a thread with a synchronous
stop()
method.I've seen versions like this:
class Thread1: def __init__(self): self._stop_event = threading.Event() self._thread = None def start(self): self._thread = threading.Thread(target=self._run) self._thread.start() def stop(self): self._stop_event.set() self._thread.join() def _run(self): while not self._stop_event.is_set(): self._work() def _work(self): print("working")
But I've read that atomic operations are thread safe and it seems to me that it can be done without
Event
. So I came up with this:class Thread2: def __init__(self): self._working = False self._thread = None def start(self): self._working = True self._thread = threading.Thread(target=self._run) self._thread.start() def stop(self): self._working = False self._thread.join() def _run(self): while self._working: self._work() def _work(self): print("working")
It think that similar implementation would be considered incorrect in C, because compiler can put
_working
to a register (or even optimize out) and the working thread would never know that the variable has changed. Can something like that happen in Python? Is this implementation correct? I don't aim to avoid events or locks altogether, just want to understand this atomic operations thing.
原文:https://stackoverflow.com/questions/23130399
最满意答案
你的映射是错误的。 你有什么不是请求参数(URL将是
/api/departments/deleteSelectedDepartments?departmentIds=1517,1518
)。你有什么路径变量:
@DeleteMapping("/departments/deleteSelectedDepartments/{departmentIds}") @Timed public ResponseEntity<Void> deleteSelectedDepartments(@PathVariable String departmentIds) {
Your mapping is wrong. What you have there is not a request param (the URL would then be
/api/departments/deleteSelectedDepartments?departmentIds=1517,1518
).What you have there is a path variable:
@DeleteMapping("/departments/deleteSelectedDepartments/{departmentIds}") @Timed public ResponseEntity<Void> deleteSelectedDepartments(@PathVariable String departmentIds) {
相关问答
更多-
CURL邮政电话 curl -i -H "Content-Type: application/json" -X POST -d '{"id":100,"username":"JohnBlog","name":"John","lastName":"Blog","email":"JohnBlog@user.com"}' http://localhost:8080/[YOURWEBAPP]/api/user/add 不同的错误情景: 在这里,我会探索在进行卷曲调用之后可能遇到的不同错误,以及可能发生错误的错误。 ...
-
我通过为我的应用程序创建自定义异常并使用@exception处理程序为每个异常处理程序提供一个异常处理程序方法来解决此问题。 我还使用了方面来确保将每个异常转换为任何自定义异常。 @Aspect @Component public class ExceptionInterceptor { @AfterThrowing(pointcut = "within(x.y.package..*)", throwing = "t") public void toRuntimeException(Thr ...
-
如果我理解正确,你可以做这样的事情(我使用的是Spring 3.2.3.RELEASE&Jackson 1.9.12)。 在您的applicationContext.xml中,您有:
如何在spring-mvc中使用相同的id接受多个REST查询参数?(How to accept mulitple REST query parameters with same id in spring-mvc?)[2021-09-05]
?id=12&id=15&id=88应该使用@RequestParam(value="id") Listids自动工作@RequestParam(value="id") List ids ?id=12&id=15&id=88 should work automatically with @RequestParam(value="id") List ids (Spring MVC)如何从Rest API中检索多个对象((Spring MVC) How to retrieve multiple objects from Rest API)[2023-07-07]
你的映射是错误的。 你有什么不是请求参数(URL将是/api/departments/deleteSelectedDepartments?departmentIds=1517,1518 )。 你有什么路径变量: @DeleteMapping("/departments/deleteSelectedDepartments/{departmentIds}") @Timed public ResponseEntitydeleteSelectedDepartments(@PathVariable St ... 我使用JQuery AJAX POST提交做了类似的事情。 你可以做这样的事情。 这会将POST请求作为JSON格式提交给控制器并等待响应。 在此等待期间可以显示进度UI组件。 //Start Progress display function setStatistic(){ var data = JSON.stringify(//build your ChecksDto) ...REST和spring-mvc(REST and spring-mvc)[2022-01-03]
为简化起见 - 您基本上有两个选择:1)构建Spring MVC应用程序。 2)构建REST后端应用程序。 如果是第一个选项 - 在您的应用程序中,您将拥有后端和前端(MVC部分)。 如果是第二种选择,则只构建后端应用程序并通过REST API公开它。 在大多数情况下,您需要为应用程序构建另一个应用程序 - REST客户端。 这是更灵活的应用程序,因为它使您有机会从各种客户端访问您的后端应用程序 - 例如,您可以拥有Android,IOS应用程序,您可以使用Angular等实现Web应用程序...... 请 ...您需要使用@RequestBody将请求的主体绑定到变量。 你需要一个物体来捕捉整个身体。 @RequestMapping(value = {"/codetype-create"}, method = RequestMethod.POST) public ResponseEntitypostCreateCodeType(ModelMap model, HttpServletRequest request, @RequestBody CompleteRequestBody body ) { ... REST API的Spring安全性(spring security for REST API)[2022-07-15]
我可以参与深入研究春季安全文档以及Eugen Parashiv的博客(当然也是书中),您可以在http://www.baeldung.com找到 REST API的Spring Security 我可以告诉Spring Security是一个很好的工具来保护许多应用程序,而不仅仅是网络应用程序! SWT或SWING也是,因为端点安全技术:) I can involve to look deeply inside the spring security docs and inside the blog (an ...可能的答案 (如果您找到更好的解决方案,请提供一个) 基于相同的骨架创建几个简单的API @RequestMapping(value = "/rest/class1", method = RequestMethod.POST, consumes = { "application/json" }) public void postData(@RequestBody Class1 objOfClass1){ store(objOfClass1) } @RequestMapping(value = "/ ...相关文章
更多- Hadoop Operations
- Storm-源码分析-Topology Submit-Executor-mk-threads
- Spring Data: a new perspective of data operations
- python字典操作
- Python 列表(list)操作
- Mod_python: The Long Story
- Python 字符串操作
- 采用Python来访问Hadoop HSFS存储实现文件的操作
- python2和python3的区别
- 探索 Python,第 1 部分: Python 的内置数值类型
最新问答
更多- 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
- 如何通过引用返回对象?(How is returning an object by reference possible?)
- 矩阵如何存储在内存中?(How are matrices stored in memory?)
- 每个请求的Java新会话?(Java New Session For Each Request?)
- css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
- 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
- xcode语法颜色编码解释?(xcode syntax color coding explained?)
- 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
- 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
- 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
- 西安哪有PLC可控制编程的培训
- 在Entity Framework中选择基类(Select base class in Entity Framework)
- 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
- 电脑二级VF有什么用
- Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
- 金华英语角.
- 手机软件如何制作
- 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
- 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
- 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
- Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
- 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
- python的访问器方法有哪些
- DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
- 在Ruby中对组合进行排序(Sorting a combination in Ruby)
- 网站开发的流程?
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。