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


Groups > comp.lang.python > #81703

Re: Asyncio problem, looking for advice.

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'concurrently': 0.07; 'method.': 0.07; 'deprecated': 0.09; 'e.g.,': 0.09; 'emulate': 0.09; 'executed': 0.09; 'friday,': 0.09; 'highlighting': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'things,': 0.09; 'works.': 0.09; 'api': 0.11; 'python': 0.11; 'bug': 0.12; '[3],': 0.16; '[4]:': 0.16; '[5]': 0.16; 'benjamin': 0.16; 'concurrent': 0.16; 'deprecated,': 0.16; 'docs.': 0.16; 'etc...': 0.16; 'finishes': 0.16; 'localhost': 0.16; 'reason).': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'ssh': 0.16; 'url:detail': 0.16; 'url:issues': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'connects': 0.19; 'programming': 0.22; 'handles': 0.22; 'preferred': 0.22; 'header:User-Agent:1': 0.23; 'mind.': 0.24; 'sends': 0.24; "shouldn't": 0.24; 'subject:problem': 0.24; 'url:dev': 0.24; 'looks': 0.24; 'question': 0.24; 'first,': 0.26; 'task': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'appreciated.': 0.29; '[1]': 0.29; 'url:code': 0.29; "doesn't": 0.30; '[2]': 0.30; 'mix': 0.30; 'programming.': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'directly,': 0.31; 'provided,': 0.31; 'writes:': 0.31; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'advice': 0.35; 'connection': 0.35; 'but': 0.35; 'there': 0.35; 'dance': 0.36; 'machine.': 0.36; 'yield': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'project': 0.37; 'message- id:@gmail.com': 0.38; 'server': 0.38; 'thank': 0.38; 'connections': 0.38; 'tasks': 0.38; 'url:library': 0.38; 'writes': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'read': 0.60; 'helps': 0.61; 'url:3': 0.61; 'traffic': 0.61; 'back': 0.62; 'kind': 0.63; 'such': 0.63; 'myself': 0.63; 'reached': 0.63; 'url:p': 0.64; 'forward': 0.65; 'response.': 0.68; 'listening': 0.74; 'saw': 0.77; 'forth': 0.81; 'asynchronous': 0.84; 'end.': 0.84; 'of*': 0.84; 'short,': 0.84; 'received:89': 0.85; 'don\xe2\x80\x99t': 0.91; 'procedural': 0.91; 'subject:looking': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Akira Li <4kir4.1i@gmail.com>
Subject Re: Asyncio problem, looking for advice.
Date Sat, 29 Nov 2014 00:04:56 +0300
References <574feffa-6412-4ae3-8de0-f84b0bdba9d2@googlegroups.com> <mailman.16402.1417176727.18130.python-list@python.org> <cd749d53-9bbf-4cc0-9997-f28ceb995607@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host 89.169.229.68
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:wEwE+/Eb1MVocwWHSpmVQim+XB4=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16417.1417208785.18130.python-list@python.org> (permalink)
Lines 98
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417208785 news.xs4all.nl 2914 [2001:888:2000:d::a6]:55966
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81703

Show key headers only | View raw


Benjamin Risher <brisher777@gmail.com> writes:

> On Friday, November 28, 2014 6:12:20 AM UTC-6, Akira Li wrote:
>> Benjamin Risher writes:
>> 
>> > Hello all,
>> >
>> > I'm working on a project to learn asyncio and network programming.
>> > What I'm trying to do is forward a connection from myself to
>> > another machine.  Kind of like an asynchronous python
>> > implementation of fpipe.
>> >
>> > In a nutshell:
>> >
>> > 1 --> start a server listening on localhost
>> > 2 --> connect to server
>> > 3 --> server connects to a listening service (ssh for instance)
>> > 4 --> server handles both connections to pass traffic back and forth through each
>> >
>> > What I have now *kind of* works.  It sends data back and forth,
>> > but when I ssh to localhost -p 12345, i never get the password
>> > prompt.  It looks like one packet hangs out and doesn't get sent
>> > from what I saw in tcpdump.
>> >
>> > Any help would be greatly appreciated.
>> 
>> Do you want to emulate `ssh -L 12345:localhost:22 <host>`?
>> 
>> > Here's a link to the same code as below, just with syntax highlighting etc...
>> > http://pastebin.com/iLE4GZH3
>> 
>> There are several issue e.g., unnecessary async(), deprecated Task()
>> calls but the main issue is that _handle_client() doesn't read
>> concurrently from the server while client writes to it.
>> 
>> You could use asyncio.wait() to run several tasks in parallel
>
>> [1]
>> https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks
>> 
>> [2] http://pastebin.com/g08YaJyz
>> 


> Akira, 
>
> First, thank you very much for your response.  It helps tremendously.
> I have a question or two though, if you don't mind.
>
> You said Task() is deprecated, but it's not listed as such in the
> docs.  Is it just that it's preferred to use other methods instead of
> using Task() directly, or am I missing something?

asyncio is a provisional API [3], it may change without notice (though
it shouldn't without a reason). From asyncio docs [4]:

  Don’t directly create Task instances: use the async() function or the
  BaseEventLoop.create_task() method.

The reason is probably to support Trollius (asyncio for Python 2) [5].

> Also, just so I can wrap my head around things, I was trying to mix
> concurrent and procedural programming in the code I provided, correct?
> Do you have any advice on how to avoid mixing types again in the
> future?

In short, the main issue was that your code executed some parts
sequentially e.g., A then B:

  yield from A
  yield from B # this is not reached until A finishes

that needed to be run concurrently:

  yield from asyncio.wait([A, B])

or in general, if you don't need to wait the results:

  asyncio.async(A)
  asyncio.async(B) 

  # ... yield later, to pass the control to the event loop
    (Task._step/_fut_waiter dance ☯)

Ask, if you have any specific questions about the code
http://pastebin.com/g08YaJyz 

There is a bug at the end. info('done') should be outside the loop
(improper indent).

[3] https://www.python.org/dev/peps/pep-0411
[4] https://docs.python.org/3/library/asyncio-task.html#asyncio.Task
[5] https://code.google.com/p/tulip/issues/detail?id=185


--
Akira

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Asyncio problem, looking for advice. Benjamin Risher <brisher777@gmail.com> - 2014-11-27 06:27 -0800
  Re: Asyncio problem, looking for advice. Akira Li <4kir4.1i@gmail.com> - 2014-11-28 15:10 +0300
    Re: Asyncio problem, looking for advice. Benjamin Risher <brisher777@gmail.com> - 2014-11-28 07:51 -0800
      Re: Asyncio problem, looking for advice. Akira Li <4kir4.1i@gmail.com> - 2014-11-29 00:04 +0300

csiph-web