简体   繁体   English

不同 Python 脚本之间的共享计数器

[英]Shared Counter between different Python scripts

How do I implement a counter between two python scripts.如何在两个 python 脚本之间实现计数器。 The counter must be incremented once by each script and should be unique.计数器必须由每个脚本递增一次,并且应该是唯一的。 ie Both scripts should not read the same value and also update the same value.即两个脚本不应读取相同的值,也更新相同的值。 Counter is in an imported file and its a database field.计数器位于导入的文件及其数据库字段中。 Help!帮助!

If the counter is in the database, when you update it you should add or subtract from it rather than setting it's value.如果计数器在数据库中,当你更新它时,你应该增加或减少它而不是设置它的值。

For example rather than (assuming counter was at 5 initially and we wanted to add 5):例如而不是(假设计数器最初为 5,我们想添加 5):

update example_table set counter = 10;

Do:做:

update example_table set counter = counter+5;

That way when your scripts are running, possibly concurrently, the database will automatically account for the value to change while the script was running.这样,当您的脚本运行时(可能是同时运行),数据库将自动考虑脚本运行时要更改的值。

This does assume that the starting value of the counter doesn't really matter since you'll only be passing the delta.这确实假设计数器的起始值并不重要,因为您只会传递增量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM