Things like this should really be run in another thread, which then
communicates with the main thread. It's easy to hang, or even crash, the
main thread if it is dependent on a network connection.

For a fairly simple example of how this is done, look at the weewx-nmea-xdr
<https://github.com/tkeffer/weewx-nmea-xdr/blob/master/bin/user/nmea-xdr.py>
service. On startup, it sets up a separate thread, then connects it to the
main thread via a queue. The thread blocks on a serial port. When data
comes in, the thread packages it, then sends it down the queue to the main
thread.

You can do something similar using a socket instead of a serial port.

Now that I think about it, take a look around in the forums. Surely someone
has done something close to what you need!

-tk

On Wed, Feb 12, 2020 at 1:42 PM Tarmo <[email protected]> wrote:

> How to exit the service (to try again later) in an exception without
> crashing the main WeeWx process?
>
> Let's say, I want to connect to a host:
>    def connect(self):
>         try:
>             self.hp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>             self.hp.connect(self.host, self.port)
>         except socket.error as e:
>             logerr("Error: connection failed {}".format(e))
>             self.hp.close()
>
>
>
> Now, in case of an exception, I would like to return gracefully to the
> main WeeWx thread without crashing it and try on the next archive interval.
>
> --
> You received this message because you are subscribed to the Google Groups
> "weewx-development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/weewx-development/1ed99eeb-426e-4c6a-a4e2-90bc2b6566ea%40googlegroups.com
> <https://groups.google.com/d/msgid/weewx-development/1ed99eeb-426e-4c6a-a4e2-90bc2b6566ea%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"weewx-development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/weewx-development/CAPq0zEBo7kYfZVr7KYE2zVKH5pgrLteaVtDy5znPS%3DcjRHQP0A%40mail.gmail.com.

Reply via email to