Skip to content

Commit

Permalink
Refine the support of Python 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjohannchang committed Nov 13, 2018
1 parent 0f04b4e commit 0bdb143
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/aioserial/aioserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import sys
import array
import asyncio
import concurrent.futures
import sys
from typing import List, Optional, Union

import serial
Expand Down Expand Up @@ -45,13 +45,6 @@ def __init__(
inter_byte_timeout=inter_byte_timeout,
exclusive=exclusive,
**kwargs)

if not loop and sys.version_info < (3,7):
# If Python version < 3.7, loop is not optional because there is no
#asyncio.get_running_loop() to be used latter.
raise TypeError("{} missing 1 required argument: 'loop'".format(
self.__class__.__name__))

self._loop: Optional[asyncio.AbstractEventLoop] = loop
self._read_executor = \
concurrent.futures.ThreadPoolExecutor(max_workers=1)
Expand All @@ -60,7 +53,8 @@ def __init__(

@property
def loop(self) -> Optional[asyncio.AbstractEventLoop]:
return self._loop if self._loop else asyncio.get_running_loop()
return self._loop if self._loop else asyncio.get_running_loop() \
if sys.version_info > (3, 6) else asyncio.get_event_loop()

@loop.setter
def loop(self, value: Optional[asyncio.AbstractEventLoop]):
Expand Down

2 comments on commit 0bdb143

@Governa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I avoided this solution because asyncio.get_event_loop does not necessarily returns the same loop we are running on, and so may cause some confusion.

@mrjohannchang
Copy link
Owner Author

@mrjohannchang mrjohannchang commented on 0bdb143 Nov 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is quite common a user has to specify the loop they wants an async function to be run in. get_event_loop returns the current running loop in the current running thread first if possible, otherwise creates one then returns it. I don't see a use case that this solution will not be fit it. Can you please give an example?

Please sign in to comment.