Internally, is asyncio run_forever() basically a while True loop?

2024/11/20 2:35:13

python asyncio run_forever or while True is similar but it is a "should I do this..." question.

I am more trying to understand if the internals of python asyncio is basically a

while True:...time.sleep(1)

(or more precisely... while not stopped: loop.

Does it use also use sleep to prevent spin waits?

Answer

Check the source code? If you're in IPython, it's pretty trivial:

>>> import asyncio
>>> loop = asyncio.get_event_loop()
>>> loop.run_forever??

The core of the loop (on my 3.11 install) is:

            while True:self._run_once()if self._stopping:break

So yep, it's a while True: loop. You can check loop._run_once?? to see the source code for that, and it's much more complicated, but short version is that it's not a sleep, it's a select call that blocks (not polls) until some async task completes, adds itself to the queue, and signals the event loop that there is work to do.

https://en.xdnf.cn/q/119879.html

Related Q&A

How do I switch from python 2.6 to 2.7 by default

How do I switch from python 2.6 to 2.7 by defaultls -l /usr/bin/python* lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python -> python2.6 lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python…

How to make a flat list from nested lists? [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)Closed 1 year ago.Given a nested list of integers, implement an iterator to flatten it. Each el…

Python 2.7: import performance

currently, I am importing bunch of .py files scattered across the file system via: def do_import(name):import impfp, pathname, description = imp.find_module(name)with fp:return imp.load_module(name, fp…

change first line of a file using bash

I need to change a single line in a file. It is always in the first line of the file. It looks like:h\tn0 n1 n2 n3 n4 n5 n6 n7 n8 n9 hilu cjt 1 1000000there is a tab in all gaps except …

1 to 2 matching in two dataframes with different sizes in Python/R

please help me with this problem Ive been struggling all day lol, solution in either Python or R is fine! Please help Im really stuck!!! I have two dataframes - df1 has 44 rows, df2 has 100 rows, they …

Can someone fix this? pip install intents

Im not sure why this is failing but I need help to fix it please Ive been trying to fix it for about 2 hours now and cant figure it out. I have tried to restart my computer, look it up on google etc. b…

Calculating Average Performance Measures in M/M/1 System Using SimPy

I am seeking to calculate the average waiting times, and average service times in the following M/M/1 queueing system, but I am not able to calculate the averages. It writes down to the console each cu…

Python: Whats the difference between import X and from X import *? [duplicate]

This question already has answers here:Use import module or from module import?(23 answers)Closed 7 years ago.I use to think both are equal until I tried this:$python Python 2.7.13 (default, Dec 17 20…

Python iterate through pixels of image

Im trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Imagew=100 h=200 im=im.resize((w,h), Im…

Geocoding with Geopy and big data

I have this CSV file which im feeding with this python scriptimport csv from geopy.geocoders import OpenCagegeolocator = OpenCage() #here some parameters are needed with open(/Users/Ian/Desktop/Test02/…