Threads and sockets

Discussion in 'Python' started by loial, Aug 10, 2012.

  1. loial

    loial Guest

    I am writing an application to send data to a printer port(9100) and then recieve PJL responses back on that port. Because of the way PJL works I have to do both in the same process(script).

    At the moment I do not start to read responses until the data has been sent to the printer. However it seems I am missing some responses from the printer whilst sending the data, so I need to be able to do the 2 things at the same time.

    Can I open a port once and then use 2 different threads, one to write to the post and one to read the responses)?
    loial, Aug 10, 2012
    #1
    1. Advertising

  2. loial <> writes:

    > I am writing an application to send data to a printer port(9100) and then recieve PJL responses back on that port. Because of the way PJL works I have to do both in the same process(script).
    >
    > At the moment I do not start to read responses until the data has been sent to the printer. However it seems I am missing some responses from the printer whilst sending the data, so I need to be able to do the 2 things at the same time.
    >
    > Can I open a port once and then use 2 different threads, one to write to the post and one to read the responses)?


    That should be possible. Alternatively, you could use "asyncore" -- a
    mini framework to facilitate asynchronous communication.
    Dieter Maurer, Aug 10, 2012
    #2
    1. Advertising

  3. On 2012-08-10, loial <> wrote:

    > At the moment I do not start to read responses until the data has
    > been sent to the printer. However it seems I am missing some
    > responses from the printer whilst sending the data, so I need to be
    > able to do the 2 things at the same time.
    >
    > Can I open a port once and then use 2 different threads, one to write
    > to the post and one to read the responses)?


    By "port" I assume you mean a TCP connection using the 'socket' module?

    If so, then yes you can write using one thread and read using a
    second thread. I do that all the time.

    Sometimes it's simpler to use a single thread that uses select or
    poll, and sometimes it's simpler to use multiple threads. And you
    never know which way is best until you're half way down the wrong
    road...
    Grant Edwards, Aug 10, 2012
    #3
  4. Am 10.08.2012 15:01, schrieb loial:
    > I am writing an application to send data to a printer port(9100) and
    > then recieve PJL responses back on that port. Because of the way PJL
    > works I have to do both in the same process(script).


    If I understand that right, you are opening a TCP connection, so
    obviously this must be done in the same process, regardless of what PJL
    (whatever that exactly is) does.


    > At the moment I do not start to read responses until the data has
    > been sent to the printer. However it seems I am missing some
    > responses from the printer whilst sending the data, so I need to be
    > able to do the 2 things at the same time.


    Using TCP, that shouldn't happen, so I really wonder what exactly you
    are doing here.


    > Can I open a port once and then use 2 different threads, one to write
    > to the post and one to read the responses)?


    Yes, definitely, take a look at the select() function of the select
    module. This basically looks like this:

    (r, w, x) = select(...)
    if r:
    # read and handle incoming data
    ...
    if w:
    # write pending output data
    ...
    if x:
    # handle connection failure
    ...


    If all this is not what you are doing and what you want (which I'm not
    100% sure of) then please elaborate a bit what you're doing and what
    kind of connection you are using.

    Happy hacking!

    Uli
    Ulrich Eckhardt, Aug 13, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jupiter5F

    Threads and Sockets

    Jupiter5F, Nov 14, 2003, in forum: C++
    Replies:
    4
    Views:
    2,291
    Jupiter5F
    Nov 14, 2003
  2. Julia Goolia

    tkinter, sockets and threads together

    Julia Goolia, Sep 11, 2003, in forum: Python
    Replies:
    4
    Views:
    522
    Steve Holden
    Sep 12, 2003
  3. Gonçalo Rodrigues

    Help needed in problem with Threads and sockets.

    Gonçalo Rodrigues, Dec 29, 2003, in forum: Python
    Replies:
    0
    Views:
    265
    Gonçalo Rodrigues
    Dec 29, 2003
  4. Rod Stephenson
    Replies:
    0
    Views:
    435
    Rod Stephenson
    Jun 22, 2004
  5. Ajay

    threads and sockets

    Ajay, Oct 13, 2004, in forum: Python
    Replies:
    3
    Views:
    286
    Elbert Lev
    Oct 14, 2004
Loading...

Share This Page