sleepvlang

Wait for n amount of milliseconds to continue executing the code in V


I am building a web crawler in V to index some URLs. The problem:

I need to limit the amount of requests I send every second, and let’s say I want to send a request every two seconds.

I've used many languages before, and all of them seem to have a keyword to achieve this. For example, PHP has the sleep() function.

After reading through the documentation, I found the following (which doesn't work for me):

import time

fn main() {
   time.sleep(2000)
}

Documentation: V time.sleep

The problem is that time.sleep() seems to be used on threads, which I am not using.

Is there a way to do it, or should I simulate it with loops?


Solution

  • You can use time.sleep just like you have used it in your example (where the main function is the calling thread). Note, however, that it takes a duration in nanoseconds, not milliseconds.

    To sleep for a given amount of milliseconds, multiply by time.millisecond:

    time.sleep(2000 * time.millisecond)
    

    To sleep for a given amount of seconds, multiply by time.second:

    time.sleep(2 * time.second)
    

    Both examples will sleep for 2 seconds.