开发者

Constantly running python script, calling functions via terminal

quick question that I'm never even sure is possible :3

I have a python script, a network script that connects to a server and remains connected until I either disconnect or it kicks me (which it normally shouldn't), which is constantly receiving data 开发者_StackOverflowand doing other tasks.

I was curious if it's at all possible while the script is running, to trigger functions from within the script? Say while the script was running, if I had the urge to send some sort of data to the server, I could type it up and send it to the function that handles this?

Wasn't quite sure if it was possible or not, as I've never had to attempt or even seen it done. If it helps, I'm on Ubuntu linux running the script from the terminal.


The usual 'UNIX-way' to solve such problems is to poll or select on both the socket and the standard input file descriptors. You then handle network input on 'IN' event on the socket and terminal input on 'IN' event on the stdin file descriptor.

This is not portable to Windows (which sucks), but that is the most natural way to do it on UNIX-like systems. And you don't get all the problems which come with threads (which often need polling in Python too, as they get 'unkillable' otherwise).


Take a look at gevent:

gevent is a coroutine-based Python networking library that uses greenlet to provide a high-level synchronous API on top of the libevent event loop.

and gevent.socket.


Jacek Konieczny's solution is good and simple. Should you want more flexible message passing, consider ZeroMQ. This gives you lots of power to easily create various messaging solutions around your main program. Using a single thread, your main program would look something like this:

#!/usr/bin/env python

import zmq
from time import sleep

CTX = zmq.Context()

incoming = CTX.socket(zmq.PULL)
incoming.bind("tcp://127.0.0.1:3000")

outgoing = CTX.socket(zmq.PUB)
outgoing.bind("tcp://127.0.0.1:3001")

# Poller for the incoming messages
poller = zmq.Poller()
poller.register(incoming, zmq.POLLIN)

def main():
    while True:
        # Do things on the network
        print("[Did things on the network]")
        # Send messages if you want
        outgoing.send("Important message")
        # Poll for incoming messages
        socks = dict(poller.poll(zmq.NOBLOCK))
        if incoming in socks and socks[incoming] == zmq.POLLIN:
            message = incoming.recv()
            # Handle message
            print("[Handled message '%s']" % message)

        sleep(1) # Only for this dummy program

if __name__ == "__main__":
    main()

You would then write a client (in any language that has ZeroMQ bindings) that pushes and subscribes to messages from the main program. Example pusher:

#!/usr/bin/env python

import zmq

CTX = zmq.Context()

pusher = CTX.socket(zmq.PUSH)
pusher.connect("tcp://127.0.0.1:3000")

def main():
    pusher.send("Message to main program")

if __name__ == "__main__":
    main()

Example subscriber:

#!/usr/bin/env python

import zmq

CTX = zmq.Context()

subscriber = CTX.socket(zmq.SUB)
subscriber.connect("tcp://127.0.0.1:3001")
subscriber.setsockopt(zmq.SUBSCRIBE, "")

def main():
    while True:
        msg = subscriber.recv()
        print("[Received message] %s" % msg)

if __name__ == "__main__":
    main()

It sounds like you will want to combine the pusher and subscriber programs into one. If you decide to use ZeroMQ have a look at the excellent user guide.

You can of course also use ZeroMQ with multiple threads or processess (just be careful not to share individual ZeroMQ sockets between threads).


Without more details, I can only provide you with general ideas. In order to do two things at once (download from the server and wait for data to send) you will need to use either multiple threads or processes. There is a tutorial with some examples of multiple threads here. If you use multiple processes, you would be using the multiprocessing package.

With either solution, you would need a similar setup. I'll use the term thread for the rest, but you could easily replace that with process if you used multiple processes instead. You would probably have (at least) a thread to send and receive data (this might be two threads) and a separate thread to wait for something to send. This is a simplified example of the producer/consumer problem. The thread that waits for the commands/data would be a simple input loop that produces data to send, while the thread that sends data would consume the data as it sends it to the server.


Stick your server stuff in another thread (investigate the threading module) and use the main thread for interaction with the user via raw_input/input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜