-
Notifications
You must be signed in to change notification settings - Fork 145
Made globals into Class properties #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
This makes file cache local to each instance of device class, which defies the purpose of file cache. |
|
I got stuck last night trying to get Bluetooth PAN working reliably, hopefully I'll have some time to look at this in the afternoon. The file cache, as @ddemidov points out, should be a class property. Arguably, the files backing the attributes of each device can be cached per device class instance. The downside of this is it's harder to manage expiring cached handles because they are not all in one place :-) I don't have this implemented yet because I have not run out of file handles for a single process, but that's a settable kernel limit, so we need a plan for when we hit it. |
|
What is the point of caching exactly? The filesystem is only parsed once, at the instantiation of a Device, which is not a time critical process. Or am I overlooking something? |
|
I see... And a per-device cache uses up too much memory? Are files much shared between devices? If not, it shouldn't have too much impact on memory and lead to cleaner code. |
|
Per-device cache should work in principle, since each device reads its own node in sysfs. As @rhempel said, it would make things harder to manage in the future, when we want to e.g. limit number of open filehandles. But there is another problem: __EV3_MODULE_connected is a global dictionary that is used to skip devices that were already connected. The patch would definitely break this functionality, although I think it is somewhat broken anyway. EDIT: It seems that By the way, what did you mean by "lib would not compile without this"? What was the exact error? |
|
So I can remove __EV3_MODULE_connected from the code? (The error is Too much files open... isn't that the problem of the user of the library? The user can always |
|
I'd even say you make the problem worse, as you skip python's built in garbage collection when you use globals. |
|
It keeps nagging at me. Another clean solution would be to have a per-device limit of open files. We could make that a smart limit: keep critical files like 'position' open and close less critical files like 'commands'. |
|
You could also move file cache into class A:
d = {}
def __init__(self, i):
if i in A.d: A.d[i] += 1
else: A.d[i] = 1
a = A(42)
b = A(101)
c = A(42)
print(A.d) |
|
Believe me, it nags on me as well. @antonvh is correct, in most cases only a few of the files would need to be read or written frequently. We can handle this a couple of ways.
|
|
That is exactly what I did in the pull request. I didn't do the smart cache yet, but file cache is in the device scope now, so that intelligence can easily be added later. Python garbage collection will clean and close files automaticilly at the end of script. If you want it earlier you can simply do: |
|
@antonvh, I merged in quite a few of your changes, but have not tested them. The work you did to fix up the getters and setters I implemented in the |
Lib wouldn't compile unless I did this.