我读过一篇关于 Python 中多线程的文章,其中他们尝试使用同步来解决竞争条件问题。我运行了下面的示例代码来重现竞争条件问题:
import threading
# global variable x
x = 0
def increment():
"""
function to increment global variable x
"""
global x
x += 1
def thread_task():
"""
task for thread
calls increment function 100000 times.
"""
for _ in range(100000):
increment()
def main_task():
global x
# setting global variable x as 0
x = 0
# creating threads
t1 = threading.Thread(target=thread_task)
t2 = threading.Thread(target=thread_task)
# start threads
t1.start()
t2.start()
# wait until threads finish their …Run Code Online (Sandbox Code Playgroud) 我有两个电话到chrome.storage.local.get(). 在继续continueCode()为我的 chrome 扩展程序执行其余代码(调用函数)之前,我需要完成这些调用,但我不确定如何执行此操作,这是我的代码。
function getData() {
chrome.storage.local.get(['key'], function(result) {
if (Object.values(result)[0] != undefined) {
object1.innerHTML = Object.values(result)[0].val;
}
});
chrome.storage.local.get(['key2'], function(result) {
if (Object.values(result)[0] != undefined) {
object2.innerHTML = Object.values(result)[0].val;
}
});
continueCode();
}
Run Code Online (Sandbox Code Playgroud) 你好 Django 社区,
当用户进行身份验证时,我想将用户的电子邮件和用户 ID 与令牌一起发回。我想我必须更改 UserLoginApiView 类,但我不知道如何覆盖 ObtainAuthToken 类来做到这一点。
有人有什么建议会很有帮助吗?
class UserLoginApiView(ObtainAuthToken):
"""Handle creating user authentication tokens"""
renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
Run Code Online (Sandbox Code Playgroud)
这是我在 Github 上的全部代码:https : //github.com/KrestouferT/profiles-rest-api