select.select issue for sockets and pipes

为君一笑 提交于 2020-01-15 18:32:08

问题


I am currently in the process of writing a basic python script that makes use of both pipes and sockets. The pipe currently holds incoming data from an html form, the socket makes up a connection to a server that sends TCP/IP commands at various intervals. The form and server are on the same LAN but different computers.

The code I have is as follows:

#!/usr/bin/env python
import os,sys,time
from socket import *

HOST = 'xxx.xxx.xxx.xxx'
PORT = 6000
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
count = 0
pipe_name = 'testpipe'

if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)

def readpipe():
    pipein = open(pipe_name, 'r')
    line = pipein.readline()[:-1]
    print line
    pipein.close()

def readTCP():
    data = tcpCliSock.recv(BUFSIZ)
    print data

while count == 0:
    readTCP()
    readpipe()

I realise the count variable is currently redundant, I have been using it to try and debug some earlier issues

As you can see the code is pretty straightforward and works as long as data comes in first on the pipe then the socket and keeps this pattern. The problem is that it is not dynamically running through both functions. It hangs while waiting for input at either the pipe of socket. I am trying to find a way to identify if no data is present on either the pipe or socket, then it can go off and check the other. Everything I have tried so far has had no effect such as checking for an empty string.

I have found some interesting stuff on the following post but it seems quite a bit different to what I am doing (using stdin) so I'm not sure if it is what I'm really looking for or whether it could be adapted;

Visit What's the best way to tell if a Python program has anything to read from stdin?

Following on from the advice by 'User' I have now put together a bit of additional code:

while True:
   inputdata,outputdata,exceptions = select.select([tcpCliSock],[],[])
   if tcpCliSock in inputdata:
      data = tcpCliSock.recv(BUFSIZ)
      print data

although this works I have not been able to discover what identifier to put into inputdata for the pipe. I have read up on the select module and apparently it does work with pipes but there seems to be very little info on it, it's all networked stuff. I have tried the following (taken from main code above):

 testpipe
 pipe_name
 pipein

But i get various errors most of which are related to a NameError - not defined or TypeError: argument must be an int, or have a fileno() method


回答1:


Usually you would use the select module for this.

import select
sockets_with_read_data, sockets_with_write_data, sockets_with_some_data = \
    select.select([sock, pipe], [], [], timeout = None)

But I do not know wether this works with pipes.



来源:https://stackoverflow.com/questions/18718355/select-select-issue-for-sockets-and-pipes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!