Giter Club home page Giter Club logo

Comments (7)

wisecsj avatar wisecsj commented on May 14, 2024

Add time.sleep(interval) in the end of async def fetch 😏

from gain.

elliotgao2 avatar elliotgao2 commented on May 14, 2024

@Jie-OY 666.
But is this works well?

from gain.

wisecsj avatar wisecsj commented on May 14, 2024

(sorry for my late reply firstly)
No...It's not works as expected

from gain.

rainfd avatar rainfd commented on May 14, 2024

Why not asyncio.sleep?
If yes,what's the reason?

from gain.

wisecsj avatar wisecsj commented on May 14, 2024

@rainfd
You inspired me!

As for why,you can test with the following method if you argee with me. 😄

@gaojiuli @rainfd
I think,before how to solve the problem,we more should get a way to test whether the solution is right.

I used wrong way to test before: add timestamp when write data stream to file as the follow shows:

await f.write(str(datetime.datetime.now())+' '+self.results['title']+'\n')

And just now i found it's wrong obviously.

Now,i think the right test way is to print timestmap in async def fetch.The excerpt as follows(assume the interval is 1s):

async def fetch(url, spider, session, semaphore):
    with (await semaphore):
        try:
            if callable(spider.headers):
                headers = spider.headers()
            else:
                headers = spider.headers
 
            time.sleep(1)
            print(datetime.now())
            async with session.get(url, headers=headers) as response:

what do you think about the test way?And the results are as expected(But still has a problem,the program will not exit finally).

from gain.

elliotgao2 avatar elliotgao2 commented on May 14, 2024

@rainfd @Jie-OY
The asyncio.sleep() could limit the interval of the start time of two requests, not limit the interval between the end time of first request and the start time of the second request, which not our purpose.
A request shouldn't be sent before the previous request finished if we set the interval.

from gain.

wisecsj avatar wisecsj commented on May 14, 2024

@gaojiuli

I have different opinions. Whether you put asyncio.sleep() before or behind async with session.get(url, headers=headers) as response: ,both couldn't limit the interval of the start time of two neighboring requests. And the two results are quite same that serveral requests would be sent almost at the same time at intervals.

As for

A request shouldn't be sent before the previous request finished if we set the interval.
limit the interval between the end time of first request and the start time of the second request

I know that my solution can only limit the interval of the start time of two requests, but why not this?

I thought set interval is just to limit the interval of the start time of two requests...:stuck_out_tongue:

from gain.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.