我有一个简单的类来获取URL,同时使用单独线程上的新循环定期更新访问令牌:
import asyncio
import aiohttp
from threading import Thread
from random import randint
LOOP = asyncio.get_event_loop()
NEW_LOOP = asyncio.new_event_loop()
t = Thread(target=NEW_LOOP.run_forever)
t.start()
class SOME_CLASS(object):
def __init__(self, loop=LOOP, new_loop=NEW_LOOP):
self.token = None
self.loop = loop
self.new_loop = new_loop
self.semaphore = asyncio.Semaphore(3)
self.session = aiohttp.ClientSession(loop=self.loop)
asyncio.run_coroutine_threadsafe(self.get_token(), self.new_loop)
def _get_headers(self):
headers = {
'x-access-token': str(self.token),
'content-type': 'application/json',
}
return headers
async def get_token(self):
while True:
self.token = randint(1, 100)
await asyncio.sleep(2)
async def fetch(self, url):
async with self.semaphore:
headers = self._get_headers()
async with self.session.get(url, headers=headers) as response:
await response.read()
return headers
async def fetch_all(self):
urls = ['https://httpbin.org/get?x={i}'for i in range(1000)]
futures = asyncio.as_completed([self.fetch(url) for url in urls])
for future in futures:
await asyncio.sleep(1)
headers = await future
print(f'headers: {headers} token: {self.token}')
def run(self):
self.loop.run_until_complete(self.fetch_all())
if __name__ == '__main__':
sc = SOME_CLASS()
sc.run()
但是,我注意到即使self.token
确实每两秒更新一次,headers
中存储的令牌也保持不变。似乎self._get_headers()
提前被调用好,而不是在它获得信号量之后:
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 8
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 8
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 78
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 78
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 41
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 56
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 56
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 74
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 74
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 4
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 4
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 10
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 10
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 44
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 44
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 28
headers: {'x-access-token': '8', 'content-type': 'application/json'} token: 28
如何在发送http请求之前确保令牌在headers
内实际更新?
答案 0 :(得分:1)
asyncio.sleep(2)
高设置为0.2,将调试打印放在fetch中并查看更新
答案 1 :(得分:1)
你有一个基本Python问题的复杂包装器。变量self.token
是一个简单的整数。存储在headers
字典中的值是此简单整数的字符串表示形式。当你将self.token重新分配给另一个整数时,程序无法知道你想要同时更改哪些其他对象。
一种解决方案是使self.token
成为具有内部状态的对象。该对象可以在程序的不同位置引用。当您更改其内部状态时,它当然会立即生效。这是一个用class Token
来说明这个的小程序。我希望你能适应你更复杂的情况。我不认为使用协同程序会影响这一点。我认为,如果您将x-access-token
的值分配给self.token
(现在是Token
的实例),那么您就可以找到解决方案了。
import random
# This way doesn't work
class MyClass:
def __init__(self):
self.token = random.randint(1, 100)
def change_token(self):
self.token = random.randint(1, 100)
c = MyClass()
a = c.token
for _ in range(10):
c.change_token()
print(a, c.token) # a does not change when c.token does
# This works
class Token:
def __init__(self, n):
self.token = n
def __repr__(self):
return str(self.token)
def set_token(self, n):
self.token = n
class MyClass2:
def __init__(self):
self.token = Token(random.randint(1, 100))
def change_token(self):
self.token.set_token(random.randint(1, 100))
print()
c = MyClass2()
a = c.token
for _ in range(10):
c.change_token()
print(a, c.token) # a and c.token are the same object