Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 确保asyncio/aiohttp协程中生成唯一的时间戳_Python 3.x_Timestamp_Python Asyncio_Aiohttp - Fatal编程技术网

Python 3.x 确保asyncio/aiohttp协程中生成唯一的时间戳

Python 3.x 确保asyncio/aiohttp协程中生成唯一的时间戳,python-3.x,timestamp,python-asyncio,aiohttp,Python 3.x,Timestamp,Python Asyncio,Aiohttp,我正在用aiohttp重写一个网页。在某些情况下,它必须使用有效负载发出POST请求,特别是包括“当前时间戳”ID。这些请求似乎总是成功的,但有时它们会被重定向(302状态代码)到另一个位置,因为需要获取更多的详细信息才能显示在页面上。这些重定向经常失败(“出现系统错误”或“未授权”错误消息显示在页面上),我不知道为什么 我猜这是因为它们有时共享“当前时间戳”ID的相同值(因为头和cookie是相同的)。因此,我希望在每个请求中生成不同的时间戳,但没有成功。我试着对asyncio.sleep(1

我正在用aiohttp重写一个网页。在某些情况下,它必须使用有效负载发出POST请求,特别是包括“当前时间戳”ID。这些请求似乎总是成功的,但有时它们会被重定向(302状态代码)到另一个位置,因为需要获取更多的详细信息才能显示在页面上。这些重定向经常失败(“出现系统错误”或“未授权”错误消息显示在页面上),我不知道为什么

我猜这是因为它们有时共享“当前时间戳”ID的相同值(因为头和cookie是相同的)。因此,我希望在每个请求中生成不同的时间戳,但没有成功。我试着对asyncio.sleep(1+(randint(5002000)/1000))等东西使用一些随机性。另外,请注意,在task_limit=1的情况下执行刮取操作会成功(请参阅下面的代码)

以下是我的代码的相关部分:

async def search(Number, session):
    data = None
    loop = asyncio.get_running_loop()
    while data is None:
        t = int(round(time() * 1000)) #often got the same value here
        payload = {'Number': Number,
                   'CURRENT_TIMESTAMP_ID': t}
        params = {'CURRENT_TIMESTAMP_ID': t}
        try:
            async with session.post(SEARCH_URL, data=payload, params=params) as resp:
                resp.raise_for_status()
                data = await resp.text()
                return data
        except aiohttp.ClientError as e:
            print(f'Error with number{Number}: {e}')
它叫via:

async def main(username, password):
    headers = {'User-Agent': UserAgent().random}
    async with aiohttp.ClientSession(headers=headers) as session:
        await login(session, username, password)
        """Perform the following operations:
        1. Fetch a bunch of urls concurrently, with a limit of x tasks
        2. Gather the results into chunks of size y
        3. Process the chunks in parallel using z different processes
        """
        partial_search = async_(partial(search, session=session)) #I'm using Python 3.7
        urls = ['B5530'] * 3 #trying to scrape the same URL 3 times
        results = await ( #I'm using aiostream cause I got a huge list of urls. Problem also occurs with gather. 
                stream.iterate(urls)
                | pipe.map(partial_search, ordered=False, task_limit=100)
                | pipe.chunks(100 // cpu_count())
                | pipe.map(process_in_executor, ordered=False, task_limit=cpu_count() + 1)
        )

希望有人能看到我错过了什么

对当前时间戳ID有什么要求?它必须是整数秒吗?它可以是表示1970年以来的纳秒的浮点值或整数值,而不是1970年以来的秒数吗?抱歉,忘了说它确实必须是整数值。我相信它是在用户浏览浏览器时使用jQuery.now()生成的。它必须反映实际的当前时间吗?我们可以在第一个请求中将其设置为1、2、3等等吗?我尝试了,但失败了。我想它必须反映当前的时间。我开始怀疑这些时间戳是否真的导致了这个问题……也许是因为你在一个会话中发出了所有请求?您是否尝试过为每个线程创建单独的会话?