1.背景描述:用Swoole作为TcpServer,配置了worker_num = 8 ,task_worker_num =8 .也就是会有16个进程。 创建了一个redid连接,在 onTask(8个task,每个task都会回调onTask方法)方法中读取redis保存的值。
2.问题描述:现在发现会报错Uncaught exception 'RedisException' with message 'read error on connection' 。初步判定是多个进程调用redid导致的,我只用一个task,是没有问题的。
看到swoole的文档:
需要在onWorkerStart中建立redis连接,那么有work 、task 各8个,如果这样的话就会有16个redis连接(task、work启动时都会调用onWorkerStart函数)。
1)需要建立16个redis连接,我这样理解对吗?该如何改进呢?
2)每个redis连接我都要保存吗?这样岂不是用起来有点麻烦。
3)如果我以后work、task的数值增加,岂不会有更多的redis连接,我该怎么办呢?
4)是不是我使用方式有问题,那么我该怎么使用呢?
回复内容:
1.背景描述:用Swoole作为TcpServer,配置了worker_num = 8 ,task_worker_num =8 .也就是会有16个进程。 创建了一个redid连接,在 onTask(8个task,每个task都会回调onTask方法)方法中读取redis保存的值。
2.问题描述:现在发现会报错Uncaught exception 'RedisException' with message 'read error on connection' 。初步判定是多个进程调用redid导致的,我只用一个task,是没有问题的。
看到swoole的文档:
需要在onWorkerStart中建立redis连接,那么有work 、task 各8个,如果这样的话就会有16个redis连接(task、work启动时都会调用onWorkerStart函数)。
1)需要建立16个redis连接,我这样理解对吗?该如何改进呢?
2)每个redis连接我都要保存吗?这样岂不是用起来有点麻烦。
3)如果我以后work、task的数值增加,岂不会有更多的redis连接,我该怎么办呢?
4)是不是我使用方式有问题,那么我该怎么使用呢?
对,每个进程都要创建一个连接。如果启动了200个进程就一定需要200个连接。你的使用方式没错。
这样使用确实会创建16个连接。
不想保存连接的话,可以在用到redis时再创建连接,用pconnect方式。
由于不同进程的redis连接不能共享,随着进程数增加是会有更多的redis
可以在worker进程中写主要逻辑。涉及redis操作的部分封装成任务,由task进程执行。在启动task时,使用taskwait方法,等待任务的返回。这样相当于把task进程当成了连接池。
如果还有其他的慢速任务需要用task异步处理,则可以在启动task时,传入指定workerid,这样其实就是把task进程区分功能,有的负责redis数据处理,有的负责慢速任务。
1: redis和mysql机制不一样, 不怕连接多。
2: 没必要用pconnect, swoole本身是长驻内存的,connect之后,除非是worker重启了,连接才失效。
3: read error on connection, 只能你复用了同一个连接,但这个连接可能在其他的进程是被close了。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

Dreamweaver Mac version
Visual web development tools
