<div dir="ltr">Scusa cosa intendi per consumare il task con gather?<div>Io per il momento lo sto semplicemente cancellando, perchè se non ho connessione col server non mi interessa che fine fa, però vorrei capire cosa intendi col gather.</div><div><br></div><div>Grazie</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-11-09 9:41 GMT+01:00 Christian Barra <span dir="ltr"><<a href="mailto:barrachri@gmail.com" target="_blank">barrachri@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="word-wrap:break-word"><br></div></div></div></div></div></div></div></div></div></div></div>
</div>
<br><div><span class=""><blockquote type="cite"><div>On 9 Nov 2017, at 07:40, Vinny Mautone <<a href="mailto:vinny74man@gmail.com" target="_blank">vinny74man@gmail.com</a>> wrote:</div><br class="m_739454223121566030Apple-interchange-newline"><div><div dir="ltr">Questo è _run_once()<div><br><div><div>Help on method _run_once in module asyncio.base_events:</div><div><br></div><div>_run_once() method of asyncio.windows_events._<wbr>WindowsSelectorEventLoop instance</div><div> Run one full iteration of the event loop.</div><div> </div><div> This calls all currently ready callbacks, polls for I/O,</div><div> schedules the resulting callbacks, and finally schedules</div><div> 'call_later' callbacks.</div></div></div><div><br></div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div>Girando su i vari forum che parlano dell'argomento ho letto che era una soluzione l'ho provata e funziona.</div></div></div></blockquote><div><br></div><div><br></div><div><br></div></span>L’underscore all’inizio di _run_once, mi fa pensare che non sia qualcosa da utilizzare…<div><br></div><div><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word">Hai per caso un link alla documentazione?</div><div style="word-wrap:break-word"><br></div><div style="word-wrap:break-word"><br></div></div></div></div></div></div></div></div></div></div></div></div><span class=""><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>Per il client ho usato una classe che eredita da asyncio.protocol, all'interno della quale c'è una funzione predefinita</div><div>connection_lost(), ed è li che fermo il loop con loop.stop()</div></div></div></blockquote><div><br></div></span><div>Perche fermi il loop dall’interno di un future?</div><div><br></div><div><a href="https://docs.python.org/3/library/asyncio-protocol.html#asyncio.BaseProtocol.connection_lost" target="_blank">https://docs.python.org/3/<wbr>library/asyncio-protocol.html#<wbr>asyncio.BaseProtocol.<wbr>connection_lost</a></div><div><br></div><div>Non hai più’ senso definire un async def tcp_reconnect da utilizzare come callback?</div><span class=""><div><br></div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>questo è il main dello script</div><div><br></div><div><div>if __name__ == '__main__':</div><div> while True:</div><div> try:</div><div> host ='localhost'</div><div> port = 8442</div><div> username = 'python_tcp_client\n'</div><div> loop = asyncio.get_event_loop()</div><div> loop.run_until_complete(tcp_<wbr>reconnect(username, loop, host, port))</div><div> except KeyboardInterrupt:</div><div> break</div><div> except Exception as e:</div><div> print(e)</div><div> finally:</div><div> for task in asyncio.Task._all_tasks:</div><div> task.cancel()</div><div> loop._run_once()</div><div> loop.close()</div></div><div><br></div><div>Senza questa riga " except Exception as e: " il programma non si riavviava ma si fermava per l'eccezione</div><div>"<span style="font-size:12.8px">"Event loop stopped before Future completed."</span></div><div><span style="font-size:12.8px">in questo modo tramite la print() la evidenzio ma tutto prosegue.</span></div></div></div></blockquote></span></div><br></div><div>Probabilmente il while True va all’interno di tcp_connect o reconnect.</div><span>asyncio.ensure_future(</span>tcp_<wbr>reconnect(username, loop, host, port))<div>loop.run_until_complete diventa run_forever.<br><div>Except per il Keyboard diventa un pass o qualcos’altro.</div><div><br></div><div>loop._run_once() lo toglierei, come dicevo prima probabilmente stai accedendo a delle private api di asyncio.</div><div><br></div><div>Per i Tasks, devi capire se li vuoi consumare (con gather per esempio) o semplicemente cancellare.</div><div><br></div><div>Alla fine loop.close()</div></div><span class=""><div><br></div><div><br></div><div><div style="word-wrap:break-word">——</div><div style="word-wrap:break-word"><div>Christian Barra</div><div>Python Freelancer // Consultant // Trainer<br>Board member of the EuroPython Society</div><div><a href="http://www.chrisbarra.xyz" target="_blank">www.chrisbarra.xyz</a></div></div></div></span></div><br>______________________________<wbr>_________________<br>
Python mailing list<br>
<a href="mailto:Python@lists.python.it">Python@lists.python.it</a><br>
<a href="https://lists.python.it/mailman/listinfo/python" rel="noreferrer" target="_blank">https://lists.python.it/<wbr>mailman/listinfo/python</a><br>
<br></blockquote></div><br></div>