php的疑问。如果两个人同时访问同一个脚本会发生什么?
我不久前制作了一个 php 搜索引擎,现在我有这样的疑问,我已经搜索过它,并在堆栈溢出上看到了很多问题,谈论对 php 脚本的多个同时访问,但仅涉及附加、修改等。假设
我的搜索引擎(或基于用户输入生成不同响应的任何其他脚本)需要 20 秒才能结束搜索。
用户 A 访问脚本
用户 B 比用户 A 晚 10 秒访问脚本(因此脚本尚未“回答”用户 A)
会发生什么?服务器是否会“执行文件的另一个实例”,以便两个用户都会得到响应,或者是否会出现混乱?
另外,如果一个 php 脚本需要很长时间才能完成执行,并且一个人访问它然后不等待响应就离开,那么该 php 脚本是否仍然会执行它必须执行的所有操作,直到它到达末尾? (逻辑告诉我,在最后一个问题上,脚本将执行所有操作,直到结束,因为该人只是发出请求,然后等待答案到来,如果它关闭,仍然没有任何东西会阻止 php 脚本执行,但我仍然在问,因为我想完全确定)
i've made a php search engine a while ago and now i have this doubt that i already searched for it and saw a lot of questions here on stack overflow talking about multiple simultaneous accesses to php scripts but only regarding appending, modifying etc...
let's say my search engine (or any other script that based on user input generates different responses) would take 20 seconds to end searching.
user A accesses the script
user B accesses the script 10 seconds later than user A (so the script didn't 'answered' user A yet)
what will happen? will the server "execute another instance of the file" and so both users will have their responses or will something be messed up?
also if a php script takes a long time to finish execution and a person accesses it and then leaves without waiting for the response, will the php script still do everything it has to do until it reaches the end? (logic tells me that on this last question the script will do everything until it reaches the end because the person just makes the request, then waits for the answer to come and if it closes still nothing is going to stop the php script from executing, but i'm still asking because i want to be completely sure)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个人都有自己的脚本实例。如果数据被修改,仍然可能存在数据一致性问题。由于HTTP是无状态协议,你不知道如果一个人没有等待结果就离开,脚本会运行到最后。
Both persons have their own instance of your script. There could still be problems concerning data consistency if data is modified. With HTTP being a stateless protocol, you don't know if a person leaves without waiting for the result, the script will run to the end.
每个用户的会话都使用一个线程进行处理。每件事都在排队,没有任何混乱。
但在某些情况下可能会出现问题。
例如:用户请求数据库中的某个字段,但另一个用户在第一个用户的线程等待或挂起时删除了该字段。但它确实是后方的,如果发生这种情况,例外将成为王座,我们的脚本有责任处理它。
every user's session processes with a Threads. every thing is in queue and nothing messes up.
but in some conditions problems may occur.
ex: a user asks for a certain field in DB but another user deletes that field while 1st user's thread is waiting or suspended. but it is really rear and if it happens an exception will be throne and it's our scripts duty to handle it.