How to wait for any socket to have data?

2024/10/16 3:28:06

I'm implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.

I'm not sure how to implement this, I was looking at select.select but it seems to wait for all the sockets to be readable.

I'd like to avoid using multiprocessing to handle data on the sockets, I would like it to be serial in reading from each socket but read when there is data available.

How do I wait for any socket to be readable?

# psudo codesockets = [sock1, sock2, sock3]while True:if len(sockets) == 0:breakfor sock in sockets:if sock.has_data():do_stuff(sock)sockets.remove(sock)sleep(0.1)
Answer

You can use select.select for your problem:

sockets = [sock1, sock2, sock3]
while sockets:rlist, _, _ = select.select(sockets, [], [])for sock in rlist:do_stuff(sock)sockets.remove(sock)
https://en.xdnf.cn/q/117758.html

Related Q&A

Retrieving ad URLs

Im looking for a way to retrieve the ad URLs for this website. http://www.quiltingboard.com/resources/What I want to do is probably write a script to continuously refresh the page and grab the ad URLs.…

Extract data (likes) from JSON API using Python

I want to pull the number of likes for my project. Heres my code: import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf…

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…

List of tuples to nested dictionary without overriding

I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python[(a, 1),(b, true),(b, none),(a, 2),(b, true),(a, 3),(b, false)]{a: {1 : { b : (true,none)…

Rotate matplotlib pyplot with curve by 90 degrees

I have plot with one line as this:import numpy as np import matplotlib.pyplot as pla = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9]) b = np.array([i/len(a) for i in range(1,…

Reading csv file and returning as dictionary

Ive written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tupl…

Spark converting Pandas df to S3

Currently i am using Spark along with Pandas framework. How can I convert Pandas Dataframe in a convenient way which can be written to s3. I have tried below option but I get error as df is Pandas dat…

install jupyter notebook in windows

My Python version is 3.6.0 and my operating system is Windows. I want to install jupyter notebook using the order pip install jupyter. But it failed, I got the following error: