运行多个并发Python程序访问同一个数据库表(Running multiple concurrent Python programs accessing the same database tabl

编程入门 行业动态 更新时间:2024-10-07 00:24:43
运行多个并发Python程序访问同一个数据库表(Running multiple concurrent Python programs accessing the same database table)

Python中有什么允许您运行多个并发Python程序,这些程序可能会访问同一个数据库表并阻止每个程序使用完整的cpu,从而允许服务器剩下一些额外的容量吗?

Is there anything in Python that allows you to run multiple concurrent Python programs that could potentially access the same database table and to prevent each program from using the full cpu and thereby allow the server to have some additional capacity left over?

最满意答案

几个问题:

多个并发Python程序 - 请参阅http://wiki.python.org/moin/Concurrency ,我将尝试使用内置模块多处理( http://docs.python.org/2/library/multiprocessing.html ) 访问相同的数据库表 - 每个进程应该创建自己的数据库连接 - 之后并发由rdbms和/或连接/查询选项管理/或配置。 如果你真的需要在进程之间同步 - 使用Locks / Semaphores可能有所帮助。 防止每个程序使用完整的cpu - 这取决于你的进程应该做什么,我会去: 有一个主程序一直运行(主进程),暂停(time.sleep,gevent.sleep或类似)并生成和控制衍生进程(工作程序) 产生的进程完成工作(工作者) - 打开新连接,执行数据库操作并退出

我确信多处理(或其他)模块提供的一些工作流程/系统可以满足您的需求(工作人员,池,队列,管道,共享状态,同步......)。

Several issues:

multiple concurrent Python programs - see http://wiki.python.org/moin/Concurrency, for the start I would try with builtin module multiprocessing (http://docs.python.org/2/library/multiprocessing.html) access the same database table - each process should create own db connection - after that concurrency is managed by/or configured within rdbms and/or connection/query options. If you really need to sync between processes - using Locks/Semaphores could help. prevent each program from using the full cpu - it depends what your processes should do, I would go with: having one main program that runs all the time (master process), does pauses (time.sleep, gevent.sleep or similar) and spawns and controls spawned processes (workers) spawned processes do the job (workers) - open new connection, do db actions and quit

I'm sure that some of the workflows/systems provided by multiprocessing (or other) modules could fit your needs (Workers, Pools, Queues, Pipes, Shared states, Synchronization, ...).

更多推荐

本文发布于:2023-07-27 04:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据库   程序   Running   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!