-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f20d61
commit c92d747
Showing
10 changed files
with
291 additions
and
264 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import datetime | ||
import threading | ||
from rest_framework.exceptions import APIException | ||
from mongoengine import Document, StringField, DateTimeField, connect | ||
|
||
# 连接 MongoDB 数据库 | ||
connect('your_database_name') | ||
|
||
class Access_token_pool(Document): | ||
access_token = StringField() | ||
now_time = DateTimeField() | ||
|
||
@classmethod | ||
def create_tokens(cls, num_tokens): | ||
current_time = datetime.datetime.now() | ||
for i in range(num_tokens): | ||
token = cls(access_token=str(i), now_time=current_time) | ||
token.save() | ||
|
||
@classmethod | ||
def get_oldest_token(cls): | ||
current_time = datetime.datetime.now() | ||
oldest_token = cls.objects.order_by('now_time').first() # 未取到不会报错 | ||
print(oldest_token) | ||
if oldest_token: | ||
oldest_token.now_time = current_time | ||
oldest_token.save() | ||
return oldest_token # 如果不存在会为None | ||
|
||
# token_lock = threading.Lock() # 创建互斥锁 | ||
|
||
def concurrent_get_token(): | ||
try: | ||
# with token_lock: # 使用互斥锁 | ||
oldest_token = Access_token_pool.get_oldest_token() | ||
print(oldest_token.access_token) | ||
return oldest_token | ||
except Exception as e: | ||
APIException("获取 token 出错:", str(e)) | ||
|
||
|
||
# 创建测试数据 | ||
# Access_token_pool.create_tokens(50) | ||
|
||
# 模拟并发获取 token | ||
threads = [] | ||
for _ in range(100): | ||
thread = threading.Thread(target=concurrent_get_token) | ||
threads.append(thread) | ||
thread.start() | ||
|
||
# 等待所有线程完成 | ||
for thread in threads: | ||
thread.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import datetime | ||
import threading | ||
from rest_framework.exceptions import APIException | ||
from mongoengine import Document, StringField, DateTimeField, connect | ||
|
||
# 连接本地 MongoDB 数据库 | ||
connect('your_database_name') | ||
|
||
# # 连接远程 MongoDB 数据库 | ||
# from mongoengine import connect | ||
# from urllib.parse import quote_plus | ||
# | ||
# connect('novel_api_h5', host="mongodb://%s:%s@%s" % (quote_plus("aiwaves"), quote_plus("bxzn2023"), "47.96.122.196")) | ||
|
||
|
||
class Access_token_pool(Document): | ||
access_token = StringField() | ||
now_time = DateTimeField() | ||
|
||
# @classmethod | ||
# def create_tokens(cls, num_tokens): | ||
# current_time = datetime.datetime.now() | ||
# for i in range(num_tokens): | ||
# token = cls(access_token=str(i), now_time=current_time) | ||
# token.save() | ||
|
||
@classmethod | ||
def get_oldest_token(cls): | ||
current_time = datetime.datetime.now() | ||
oldest_token = cls.objects.order_by('now_time').first() # 未取到不会报错 | ||
print(oldest_token.access_token) | ||
if oldest_token: | ||
oldest_token.now_time = current_time | ||
oldest_token.save() | ||
return oldest_token # 如果不存在会为None | ||
|
||
|
||
|
||
# token_lock = threading.Lock() # 注释掉这两行代码,就变成了不加锁的版本 | ||
|
||
def concurrent_get_token(): | ||
try: | ||
# with token_lock: # 注释掉这两行代码,就变成了不加锁的版本 | ||
oldest_token = Access_token_pool.get_oldest_token() | ||
print(oldest_token.access_token) | ||
return oldest_token | ||
except Exception as e: | ||
APIException("获取 token 出错:", str(e)) | ||
|
||
|
||
# 创建测试数据 | ||
# Access_token_pool.create_tokens(50) | ||
|
||
# 模拟并发获取 token | ||
threads = [] | ||
for _ in range(100): | ||
thread = threading.Thread(target=concurrent_get_token) | ||
threads.append(thread) | ||
thread.start() | ||
|
||
# 等待所有线程完成 | ||
for thread in threads: | ||
thread.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from mongoengine import Document, StringField, DateTimeField, connect | ||
import datetime | ||
|
||
# 当前时间 | ||
current_time = datetime.datetime.now() | ||
|
||
# 连接本地 MongoDB 数据库 | ||
connect('your_database_name') | ||
|
||
|
||
# # 连接远程 MongoDB 数据库 | ||
# from mongoengine import connect | ||
# from urllib.parse import quote_plus | ||
# | ||
# connect('novel_api_h5', host="mongodb://%s:%s@%s" % (quote_plus("aiwaves"), quote_plus("bxzn2023"), "47.96.122.196")) | ||
|
||
class Access_token_pool(Document): | ||
access_token = StringField() | ||
now_time = DateTimeField() | ||
|
||
|
||
access_token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1UaEVOVUpHTkVNMVFURTRNMEZCTWpkQ05UZzVNRFUxUlRVd1FVSkRNRU13UmtGRVFrRXpSZyJ9.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL3Byb2ZpbGUiOnsiZW1haWwiOiJqaWF0a2IwNjU2QHdlcC5lbWFpbCIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlfSwiaHR0cHM6Ly9hcGkub3BlbmFpLmNvbS9hdXRoIjp7InVzZXJfaWQiOiJ1c2VyLWdGV3RRS2QxYkJuWmNsbWgzeXdrNllzSCJ9LCJpc3MiOiJodHRwczovL2F1dGgwLm9wZW5haS5jb20vIiwic3ViIjoiYXV0aDB8NjQ0YTFiMmUxZGVjOGQyZWRhOGQxZmU0IiwiYXVkIjpbImh0dHBzOi8vYXBpLm9wZW5haS5jb20vdjEiLCJodHRwczovL29wZW5haS5vcGVuYWkuYXV0aDBhcHAuY29tL3VzZXJpbmZvIl0sImlhdCI6MTY4NjExMDMzNywiZXhwIjoxNjg3MzE5OTM3LCJhenAiOiJUZEpJY2JlMTZXb1RIdE45NW55eXdoNUU0eU9vNkl0RyIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwgbW9kZWwucmVhZCBtb2RlbC5yZXF1ZXN0IG9yZ2FuaXphdGlvbi5yZWFkIG9yZ2FuaXphdGlvbi53cml0ZSJ9.0fI-3RYbF1eg_kRzE3Q3QYNGa6kgOgsaPvt4m9HREjRkYFKWGVP3lBWxGQkG3553imEr2vZjETn4hyHFGj-LLYg1XpLpa-_zvhjITyzzT4flUZijFp6tzZg185WIySeXrmoKmZTxL7ij1LF1Un6Y2IRPSIbvOvugr5wWvrPZtZD0xHcgmzmj-A1xJytjM7zhdlKtaJiK_VyeR6wlTv5fLdjPWxU5nutmxSnaV6i9drR9g8HCMk2_mtckyS_zAUJIW2KQVZ81d0OFijL2aiaQiaSOtNiV2jKRr0jrm9lysUkvKDwszQMGtOZXhPR2KMWEr_nLHZpryBJdYPf3yF8E7A" | ||
# Access_token_pool(access_token=access_token,now_time=current_time).save() | ||
|
||
|
||
token_obj = Access_token_pool.objects.first() | ||
token_obj.access_token = access_token | ||
token_obj.save() |
Oops, something went wrong.