最近遇到数据量比较大并且更新速度很快的情况,这样导致两台服务器同步稍有延迟的情况出现,同时负载升高。
如果使用sleep 每10秒 休眠1秒的做法是否能缓解数据库的压力呢?我很想知道sleep是否会占用很多资源,sleep执行的时候都做了些什么事呢?
但是有人说单线程休眠时间越长占用资源就越多,是这样吗?
如果自己验证该如何切入呢?
网上搜了下关于sleep的资料但是没发现有太大价值的,实在想不通到底该怎么解决好了
回复内容:
最近遇到数据量比较大并且更新速度很快的情况,这样导致两台服务器同步稍有延迟的情况出现,同时负载升高。
如果使用sleep 每10秒 休眠1秒的做法是否能缓解数据库的压力呢?我很想知道sleep是否会占用很多资源,sleep执行的时候都做了些什么事呢?
但是有人说单线程休眠时间越长占用资源就越多,是这样吗?
如果自己验证该如何切入呢?
网上搜了下关于sleep的资料但是没发现有太大价值的,实在想不通到底该怎么解决好了
sleep不占用系统资源,它会把cpu的控制权交换给内核,内核会再schedule到另一个进程。
这种简单的事情,你写个while (true) sleep(1)的程序然后top一下就能判断的。
任何语言, sleep本身不占CPU资源, 但是在基于PHP的LAMP环境中, 就不能这样说了, 因为CPU不是唯一的资源. 进程数, 内存, 这些都是资源.
PHP不管工作工作在apache mod_php还是nginx fpm下, 总的进程数基本上是有一个限制的. 对于服务器的吞吐, 有一个很简单的算数, 你有几个线程/进程, 你就能同时处理多少http请求
但是, 一单在PHP里面sleep, 那么php处理请求的时间就等于本身处理的时间+sleep时间
本来0.2秒处理一个请求, 现在sleep了0.8秒, 那么你的php请求处理速度就变成1秒, 假设你的apache开10个进程, 那么你每秒钟处理请求的速度就由5个每秒*10个进程 50 变成了 1个每秒*10个进程.
另外你不可能在apache里面开无限的的进程, 不管是进程还是线程, 都开销很大, 1024个进程基本上就死的很惨了, 线程在5k左右(CPU使用不多, 大部分sleep的情况下)也很吃力了
如果你sleep 10秒, 整个PHP, web系统就没办法用了, 曾经有人想通过sleep来做长连接, 本机测试通过了, 但是部署上系统无异于自杀.
之前也用php写过后台方面的程序,主进程需要等待所有子进程结束后再退出,之前采取的“忙等”,发现主进程将CPU占完了,后来主进程在等待循环中采用了sleep(1)的方式,大大减少了cpu开销,所以我认为sleep应该不会占用太多CPU。仅供楼主参考!
@felix021 和 @KJ 说的都对,你需要评估一下在sleep中,有哪些资源是占用的,比如HTTP连接,数据库连接。尤其是数据库最大并发连接通常要少于HTTP连接,需要小心连接被吃满的情况。如果是脚本下执行的PHP,那sleep是很简单靠谱的办法;如果是web,需慎重。

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
