Cloudlet can effectively reduce the computing load and communication delay of the remote cloud. However, since the cloudlet does not have the powerful computing performance of the remote cloud, as the number of users changes, the resources that the cloudlet can provide for each user change. In addition, as the user has mobility and the service coverage of the cloudlet is limited, the user may get out of the service coverage of the cloudlet during the task execution. In this case, the user will not receive the calculation results, which will lead to the failure of cloud computing. In order to allocate the necessary and sufficient resources to the users, this paper proposes a real-time resource allocation framework. A task movement record-based particle swarm optimization (MRPSO) algorithm is introduced to solve the problem of real-time resource allocation and task failure. Experiments show that the proposed method can provide an effective solution which performs faster than the original PSO method.