Python的多线程 [英] Python multithreading

查看:185
本文介绍了Python的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的情况:

使用的Zope / Plone的一些矿山的Python API创建的网页。有一个网页,称之为一,即由一个python方法调用的数据库(Postgres的)并返回一些信息。在页面的A,你可以修改数据库中的数据离线(我打算所做的更改不会写入数据库瞬间,但在当你preSS第二时刻拯救,并调用一个Python API的方法)。所以,想象一下这样的场景:一个用户名为萨姆,加载页面,并开始修改数据。同时一个用户名为莎拉,修改了A页点击保存数据库。现在,萨姆不具有实际的数据库中的数据:他会按保存,并覆盖Sara的数据更改

A web page created with Zope/Plone and some mine python API. There's a web page, call it "a", that by a python method calls a database (Postgres) and returns some information. On page "a" you can modify database data "offline" (I intend that the changes aren't written in the database instantly but in a second moment when you press "save" and call a python API method). So, imagine this scenario: an user, called "Sam", loads the page and start to modify data. Meanwhile an user, called "Sara", modifies the database by the page "a" clicking "save". Now Sam doesn't have the actual database data: he'll push "save" and overwrite Sara's data change.

我会在我的实时页面警报。我想我可以做这样的事情:

I would have an alert on my page in real time. I thought I can do something like this:

请AJAX调用,这不是可阻止,并保持要与页面呈现。 Ajax调用,创建一个线程,做(在X条件),无限循环蟒蛇方法。当我写在数据库中的数据,我会打电话给一个函数,将改变X状态停止线程,并返回到AJAX。

Make an AJAX call, that isn't blockable, and keep going with page render. The AJAX calls a python method that creates a thread that does an infinite loop (on an "X" condition). When I write data on database, I'll call a function that will change "X condition" stopping the thread and returning to AJAX.

此外,我无法锁定数据库,因为我不得不放弃免费获得想要修改我的数据库中的每个用户。

Moreover, I can't lock the database because I have to give free access to every user that wants to modify my database.

我的问题是:我怎么能确定一个Python的线程?我刚刚看到一个类的每一个方法,从线程继承想自我为参数。此外,我必须调用线程,因为我访问了一页面,这将是在code某处(说的线程模块上),但刀片是其他模块。那么,如何能实现我的想法?

My problem is: how can I identify a python thread ? I've just saw that every single method on a class that inherit from Thread wants "self" as parameter. Moreover, I have to call the thread as I access the "a" page and this will be somewhere in the code (say on the "threads module") but the inserts are on the other module. So, how can I realize my idea ?

如果有人有另一种想法,告诉我没有任何问题:)

And if someone have an alternative idea, tell me without any problem :)

推荐答案

的问题,你正在讨论的领域通常被称为并发。因为你的方法将给予警告或者从什么时候在目标项目改变任何领域,这种方法通常被称为悲观并发更新阻止用户。要做到这一点的一种方法是跟踪哪些项目看,当它被选择等,并且仅当数据库版本看起来就像选择或因为在一定的时间尚未更新的版本更新(时间戳字段可能是有帮助)。您也可以尝试乐观并发,在其中只检查领域的一个用户已更新,并节省了回不是由其他用户更新的数据存储。如果你选择一个支持并发的ORM库这两种方法是最简单的。

The realm of problem you're discussing is generally called, "Concurrency". Since your method would warn or block the user from updating when any field in the target item changes, the approach is usually called "Pessimistic Concurrency". One way to do this is to keep track of what the item looked like when it was selected, and only update if the database version looks exactly like the version you selected or has not been updated since a certain time (a timestamp field may be helpful). You could also try optimistic concurrency, in which you only check that fields one user has updated and is saving back to the datastore were not updated by the other user. Both of these methods are easiest if you choose an ORM library that supports concurrency.

我最喜欢的Python网络库是Django的,这里是SO差不多的情况下,你正在寻找解决一个问题:<一href="http://stackoverflow.com/questions/320096/django-how-can-i-protect-against-concurrent-modification-of-data-base-entries">Django:我怎样才能防止数据库条目的并发修改。我希望它能帮助。

My favorite python web library is django, and here is a question on SO about the same situation you are looking to solve: Django: How can I protect against concurrent modification of data base entries. I hope it helps.

在你建议的方式处理并发是可行的,但应避免在大多数情况下。我已经加入并发一个大系统,复杂的对象已经广泛的副作用,并没有统一的数据访问时,做之前(有过的系统的生命周期的数据访问约5的方法,它是一个丰富多彩的系统)。这是错误倾向和复杂的方式来处理并发(我想我有一个客户端应用程序,并启动了一个观察家线程标记的项目后,所描述的类型和对象的标识,谁签出用户数据表签出 ,当他们检查出来,以及它是如何长的有效期为,万一谁检查对象的客户端出来没有检查它的时候完成了)。

Handling concurrency in the manner you suggest is doable but should be avoided in most situations. I've done it before when adding concurrency to a large system with complex objects that had wide ranging side effects and no unified data access (there were about 5 methods of data access over the lifetime of the system, it was a colorful system). It's bug prone and complex way to handle concurrency (I think I had a client app and kicked off a watcher thread after marking items "checked out" in a data table that described the type and identifier of the object, the user who checked it out, when they checked it out, and how long it was valid for, in case the client who checked the object out failed to check it in when finished).

如果您在不使用ORM,并显示一条消息,当变化发生到该项目的用户设置,尝试将关闭最后一个更新时间戳列,只是有你的Ajax调用检查,看是否最后一次更新时间是更大的,当你第一次加载的项目比它。所以,如果你编写一个通用的方法来做到这一点,你只需要在表名,主键和时间戳。

If you are set on not using an ORM and displaying a message to the user when changes have occurred to the item, try going off a last updated timestamp column and just have your ajax call check to see if the last update time is greater than it was when you first loaded the item. So, if you were coding a generic way to do this, you would simply need the table name, the primary key, and the timestamp.

Web服务方法可能如下:

webservice method might look like:

def is_most_current(table_name, id):
    db = MySQLdb.connect(passwd="moonpie",db="thangs")
    c=db.cursor()
    c.execute("SELECT last_updated from %s where id = %s", (table_name, id))
    return c.fetchone()

对于蟒蛇多线程库,Python的线程是混乱,生产性能较差得益于问题蟒蛇的全球连锁,你实际上可能需要产生一个新的进程在很多情况下(多道库是相当相当的,并且执行并行更好处理方案)。至于自我这是为参考,以你处理的类的实例的Python的习惯,就像这个用C语言的语言。你可以给它一个唯一的名称,当你构建它容易识别的线程。请参阅多处理或的线程文档获取更多信息。如果你能避免线程对于这个问题,我建议你这样做。

As for the python multithreading libraries, python threads are confusing and produce poor performance thanks to issues with python's global interlock, you may actually want to spawn a new process in many cases (the multiprocessing library is fairly equivalent and performs better in parallel processing scenarios). As far as "self" that's a pythonic convention for the reference to the instance of the class you're dealing with, much like "this" in C like languages. You could easily identify a thread by giving it a unique name when you construct it. See the multiprocessing or threading docs for more info. If you can avoid threading for this problem, I recommend that you do so.

这篇关于Python的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆