Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26869 > unrolled thread

Threads and sockets

Started byloial <jldunn2000@gmail.com>
First post2012-08-10 06:01 -0700
Last post2012-08-13 09:15 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Threads and sockets loial <jldunn2000@gmail.com> - 2012-08-10 06:01 -0700
    Re: Threads and sockets Dieter Maurer <dieter@handshake.de> - 2012-08-10 18:38 +0200
    Re: Threads and sockets Grant Edwards <invalid@invalid.invalid> - 2012-08-10 22:38 +0000
    Re: Threads and sockets Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-13 09:15 +0200

#26869 — Threads and sockets

Fromloial <jldunn2000@gmail.com>
Date2012-08-10 06:01 -0700
SubjectThreads and sockets
Message-ID<775f30ad-1f04-4653-95c4-b0dfdd27ca49@googlegroups.com>
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)?
 

 

[toc] | [next] | [standalone]


#26879

FromDieter Maurer <dieter@handshake.de>
Date2012-08-10 18:38 +0200
Message-ID<mailman.3168.1344616733.4697.python-list@python.org>
In reply to#26869
loial <jldunn2000@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#26903

FromGrant Edwards <invalid@invalid.invalid>
Date2012-08-10 22:38 +0000
Message-ID<k042gv$24c$1@reader1.panix.com>
In reply to#26869
On 2012-08-10, loial <jldunn2000@gmail.com> 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...

[toc] | [prev] | [next] | [standalone]


#26969

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-08-13 09:15 +0200
Message-ID<96mmf9-7t.ln1@satorlaser.homedns.org>
In reply to#26869
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web