asyncore loop and cmdloop problem

K

kaklis

Hi to all,
i'm creating a command line application using asyncore and cmd. At

if __name__ == '__main__':
import socket

args = sys.argv[1:]
if not args:
print "Usage: %s querystring" % sys.argv[0]
sys.exit(0)


address = ('localhost', 0) # let the kernel give us a port
server = EchoServer(address)
ip, port = server.address # find out what port we were given

asyncore.loop()
CmdClass().cmdloop()

what i want is that the above commands asyncore.loop() and
CmdClass().cmdloop()
running at the same time. Meaning that while the application is in cmd
mode
with the cmdloop(), it is still able to listen for incoming messages?
What should i do?

thanks in advance
A.K.
 
M

Michele Simionato

Hi to all,
i'm creating a command line application using asyncore and cmd. At

if __name__ == '__main__':
    import socket

    args = sys.argv[1:]
    if not args:
        print "Usage: %s querystring" % sys.argv[0]
        sys.exit(0)

    address = ('localhost', 0) # let the kernel give us a port
    server = EchoServer(address)
    ip, port = server.address # find out what port we were given

    asyncore.loop()
    CmdClass().cmdloop()

what i want is that the above commands asyncore.loop() and
CmdClass().cmdloop()
running at the same time. Meaning that while the application is in cmd
mode
with the cmdloop(), it is still able to listen for incoming messages?
What should i do?

thanks in advance
A.K.

cmd.Cmd is blocking, so the only way it to run the cmdloop in a
separated thread. Once for fun
I rewrote the cmd module to be non-blocking but if you want to stick
with the standard library you need to use a thread.
 
K

kaklis

Hi to all,
i'm creating a command line application using asyncore and cmd. At
if __name__ == '__main__':
    import socket
    args = sys.argv[1:]
    if not args:
        print "Usage: %s querystring" % sys.argv[0]
        sys.exit(0)
    address = ('localhost', 0) # let the kernel give us a port
    server = EchoServer(address)
    ip, port = server.address # find out what port we were given
    asyncore.loop()
    CmdClass().cmdloop()
what i want is that the above commands asyncore.loop() and
CmdClass().cmdloop()
running at the same time. Meaning that while the application is in cmd
mode
with the cmdloop(), it is still able to listen for incoming messages?
What should i do?
thanks in advance
A.K.

cmd.Cmd is blocking, so the only way it to run the cmdloop in a
separated thread. Once for fun
I rewrote the cmd module to be non-blocking but if you want to stick
with the standard library you need to use a thread.

Thank you so much.
Is there a way that i can find that version of cmd?

Antonis
 
M

Michele Simionato

Too bad cmdloop() doesn't provide an argument to return immediately.
Why don't you submit this patch on the bug tracker?

--- Giampaolohttp://code.google.com/p/pyftpdlibhttp://code.google.com/p/psutil

Because it is not a bug, cmd was designed to be blocking. It would be
a feature request.
I wrote a cmd2 module a few years ago, which was intended as a
replacement for cmd with various
additional features (including the non blocking behavior). We are
using it in production, but I
have never published it (I intended to but, you know, a days has only
24 hours ;)
I should put together the code in a single file and publish it, but I
cannot guarantee if and when I will have the time to do so.
 
K

kaklis

Sure, a feature request, but it would be nice to have anyway. =)
The OP question shown how this can be desirable in certain circumstances...

--- Giampaolohttp://code.google.com/p/pyftpdlibhttp://code.google.com/p/psutil


Could you please provide me with a simple example how to do this with
threads.
I don't know where to put the cmdloop().
Please help, i' m so confused!
Thank you
 
M

Michele Simionato

Could you please provide me with a simple example how to do this with
threads.
I don't know where to put the cmdloop().
Please help, i' m so confused!
Thank you

What are you trying to do? Do you really need to use the standard
library? Likely what you want to accomplish is already implemented in
Twisted; I remember there was something like that in their examples
directory.
 
K

kaklis

What are you trying to do? Do you really need to use the standard
library? Likely what you want to accomplish is already implemented in
Twisted; I remember there was something like that in their examples
directory.

Thank you,
and sorry for the mistake i did before with my post.

Antonis
 
K

kaklis

Thank you,
and sorry for the mistake i did before with my post.

Antonis

hi again. i installed twisted, but since i m not familiar with it, do
you remember which example you have in mind?
What i want to accomplish is something like "asterisk". while you send
commands from the asterisk cli, at the same time
the underlying protocol sends you notifications to the console.
without exiting the application.
thanks
 
E

exarkun

hi again. i installed twisted, but since i m not familiar with it, do
you remember which example you have in mind?
What i want to accomplish is something like "asterisk". while you send
commands from the asterisk cli, at the same time
the underlying protocol sends you notifications to the console.
without exiting the application.
thanks

You can find a couple simple examples here:

http://twistedmatrix.com/documents/current/core/examples/

Search for "stdin" and "stdio".

Jean-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top