python多用户数据库_运行多个并发Python程序以访问同一数据库表

本文探讨了如何在Python中运行多个并发程序并访问相同数据库表的问题。建议使用内置的multiprocessing模块来实现并发,并指出每个进程应创建自己的数据库连接,数据库并发由RDBMS及其配置管理。为防止CPU过度使用,可以设计一个主程序控制工作进程,工作进程负责执行任务、数据库操作然后退出。还提到了使用Locks/Semaphores进行进程间同步的可能性。
摘要由CSDN通过智能技术生成

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?

解决方案

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, ...).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值