Firstly, I'm new to python and OOP so I'm still learning how things work.
I am trying to port a websocket API helper to a new websocket library as the original library has bugs and the github repo is inactive. The new library(autobahn-twisted) is asynchronous and works via callbacks, where the original library placed new responses onto a queue for synchronous processing. This adds another level of complexity that I am still learning.
Currently, there are 3 modules that I have created/modified. These are:
connector.py
contains Protocol class which extends WebsocketClientProtocol
Protocol handles low level interaction with API such as formatting messages for sending, and receiving and processing(low lvl) responses
client.py
contains Client class which extends Protocol
Client contains higher level functions for un/subscribing to streams, and accessing other API endpoints
user_code.py
contains userClass class
my initial design was going to have this module inheriting from Client to perform operations on, and process the response data
My problem is in the userClass class, and its interaction with the other classes. I want to be able to call userClass methods from inside the Protocol class when messages are received and processed, and call Client methods from the userClass to request messages and subscribe to streams.
For the first attempt, I created an abstract class containing all the methods I wanted to call, and used userClass to implement all of them. This (I think) meant I could safely call the child methods from the parent methods, but I could not call the Client methods from userClass without creating a circular reference, and it seemed fragile(or in other words, everything broke) when I moved things to a new module.
My second attempt had Client as an object in userClass, using the aggregation relationship. Inside Client and Protocol, I referenced the userCode class rather than the object, however I then lose references to the object when calling methods.
I have not yet attempted to use straight inheritance with userClass inheriting Client and overwriting "dummy" methods from the parent classes, as it seemed like there was a lot of code duplication.
This example shows the functionality that I would like
class Protocol(WebsocketClientProtocol)
def onOpen(self):
print("open")
connectionOpened()
def send(self, msg):
self.sendMessage(json.dumps(msg))
class Client(Protocol)
def subscribe(self, msg)
self.send("subscribe_" + msg)
class userClass(Client)
def connectionOpened(self):
subscribe("this_stream")
What design paradigm should I follow to get this behaviour?
Thanks in advance
Related
I have a problem at work where I need to perform series of sequential tasks on different devices. These devices do not need to interact with each other and also each of the sequential tasks can be performed on each of the devices independently.
Assuming I have Tasks (A->B->C->D)(Ex: End of A triggers B and end of B triggers C and so on), Devices(dev1, dev2) can execute these tasks independent of each other.
How can I design a centralized system that executes each task on each device. I cannot use Threading or Multiprocessing due to Infra limitations.
I'm looking for some design suggestions(Classes) and How I can go about designing it.
First approach I thought about was brute force where I blindly use loops to loop over devices and perform each task.
Second approach I was reading about State Design Pattern and I was not sure how I can implement it.
EDIT: I have implemented the answer I have provided below. However I would like to know the correct way to transfer information between states. I know states needs to be mutually exclusive but each task needs to access certain resources that are common amongst all the resources. How can I structure this ?
I have used State design pattern to handle this. I have a Device class which is concrete class and have a method called "perform_task". This method changes behavior based on the state it is in. At a given point it can be in TaskA TaskB or etc.
class Device():
_state = None
def __init__(self):
"""Constructor method"""
self.switch_to(TaskA())
def switch_to(self, state):
self._state = state
self._state.context = self
def perform_task(self):
self._state.perform_task()
Then I have a State Abstract class which has abstract methods. Followed by State classes itself.
class State(ABC):
#property
def context(self):
return self._context
#context.setter
def context(self, context):
self._context = context
#abstractmethod
def perform_task(self):
pass
class TaskA():
def perform_task(self):
# Do something
self.context.switch_to(TaskB())
class TaskB():
def perform_task():
# Do something.
pass
Doing so we can extend this to any number of states in the future and handle new conditions too.
I probably try something with flask for super simple api and a client app on devices that "pool" data from center api and post results so center server know the progress and what is current used. client app would be super simple loop with sleep so it wont 100% cpu without needed.
I am trying to create unit tests for a private callback factory with an alarm manager class. I don't want to test the private method directly as I think this contravenes TDD good practice but I cannot figure out a good way to test this.
Please don't just tell me my code is bad (I already know!). I just want to know if it is possible to use the unittest framework to test this or if I need to re-architect my code.
My class is an alarm manager that is responsible for managing the creation, scheduling and sound of an alarm. When an alarm is created, it is passed to a scheduler with a callback function. To create this callback function, there is a private callback factory that takes in the alarm object to generate the custom callback (while capturing some variables from the manager class)
The problem I have is that unless I use the full functionality of the scheduler, the callback will never get executed but obviously there will be a lot of pain with patching time to make sure it executes in a reasonable time. Furthermore, the callback is never returned to the user so no easy way of checking (or even calling it there)
The manager class looks largely like the code below:
class Manager:
def __init__(self):
self._scheduler = alarm.scheduler.Scheduler()
self._player = sound.player.Player()
def create_alarm(self, name, new_alarm):
self._scheduler.add_job(name, new_alarm.find_next_alarm(),\
self._create_callback(name, new_alarm))
def _create_callback(self, name, callback_alarm):
def callback():
self._player.play(callback_alarm.get_playback())
return callback
Overall, is there a way to somehow extract the callback object if I make the scheduler a mock object. Or is there some other clever way to test that the callback is doing what it should be?
I was pretty shocked to find out that "require" in node creates a singleton by default. One might assume that many people have modules which they require which have state, but are created as a singleton, so break the app as soon as there are multiple concurrent users.
We have the opposite problem, requires is creating a non-singleton, and we dont know how to fix this.
Because my brain is wired as a java developer, all our node files/modules are defined thusly:
file playerService.js
const Player = require("./player")
class PlayerService {
constructor(timeout) {
// some stuff
}
updatePlayer(player) {
// logic to lookup player in local array and change it for dev version.
// test version would lookup player in DB and update it.
}
}
module.exports = PlayerService
When we want to use it, we do this:
someHandler.js
const PlayerService = require("./playerService")
const SomeService = require("./someService")
playerService = new PlayerService(3000)
// some code which gets a player
playerService.updatePlayer(somePlayer)
Although requires() creates singletons by default, in the above case, I am guessing it is not creating a singleton as each websocket message (in our case) will instantiate a new objects in every module which is called in the stack. That is a lot of overhead - to service a single message, the service might get instantiated 5 times as there are 5 different sub services/helper classes which call each other and all do a requires(), and then multiply this by the number of concurrent users and you get a lot of unnecessary object creation.
1) How do we modify the above class to work as a singleton, as services don't have state?
2) Is there any concept of a global import or creating a global object, such that we can import (aka require) and/or instantiate an object once for a particular websocket connection and/or for all connections? We have no index.js or similar. It seems crazy to have to re-require the dependent modules/files for every js file in a stack. Note, we looked at DI options, but found them too arcane to comprehend how to use them as we are not js gurus, despite years of trying.
You can simply create an instance inside the file and export it.
let playerService = new PlayerService();
module.exports = playerService;
In this case, you may want to add setters for the member variables you would take as constructor parameters to ensure encapsulation.
Also note that, creating object instances with new in javascript is cheaper than traditional OOP language because of it's prototype model (more).
So don't hesitate when you really need new instances (as seen in your code, do you really want to share the timeout constructor parameter?), since javascript objects are pretty memory efficient with prototype methods and modern engines has excellent garbage collectors to prevent memory leak.
Please consider a scala.js application which runs in the browser and consists of a main program and a web worker.
The main thread delegates long running operations to the web worker by passing messages that contain the names of methods and the parameters required to invoke them. The worker passes method return values back to the main thread in the form of response messages.
In simpler terms, this program abstracts web worker messaging so that code in the main thread can call methods in the worker thread in idiomatic and asynchronous Scala syntax.
Because web workers do not associate messages with their responses in any way, the abstraction relies on a registry, an intermediary object, that governs each cross context method call to associate the invocation with the result. This singleton could also bind callback functions but is there a way to accomplish this with futures instead of callbacks?
How can I build an abstraction over this registry that allows programmers to use it with the standard asynchronous programming structures in Scala: futures and promises?
How should I write this functionality so that scala programmers can interact with it in the canonical way? For example:
// long running method in the web worker
val f: Future[String] = Registry.ultimateQuestion(42) // async
f onSuccess { case q => println("The ultimate question is: " + q) }
I'm new to futures and promises, but it seems like they usually complete when some execution block terminates. In this case, receiving a response from the web worker signifies completion of the future. Is there a way to write a custom future that delegates its completion status to an external process? Is there another way to link the web worker response message to the status of the future?
Can/Should I extend the Future trait? Is this possible in Scala.js? Is there a concrete class that I should extend? Is there some other way to encapsulate these cross context web worker method calls in existing asynchronous Scala functionality?
Thank you for your consideration.
Hmm. Just spitballing here (I haven't used workers yet), but it seems like associating the request with the Future is fairly easy in the single-threaded JavaScript world you're working in.
Here's a hypothetical design. Say that each request/response to the worker is automatically wrapped in an Envelope; the Envelope contains a RequestId. So the send side looks something like (this is pseudo-code, but real-ish):
def sendRequest[R](msg:Message):Future[R] = {
val promise = Promise[R]
val id = nextRequestId()
val envelope = Envelope(id, msg)
register(id, promise)
sendToWorker(envelope)
promise.future
}
The worker processes msg, wraps the result in another Envelope, and the result gets handled back in the main thread with something like:
def handleResult(resultEnv:Envelope):Unit = {
val promise = findRegistered(resultEnv.id)
val result = resultEnv.msg
promise.success(result)
}
That needs some filling in, and some thought about what the types like R should be, but that sort of outline would probably work decently well. If this was the JVM you'd have to worry about all sorts of race conditions, but in the single-threaded JS world it probably can be as simple as using an autoincrementing integer for the request ID, and storing away the Promise...
I create a CwinThread::thread in my application in order to get access to the data in COM asynchronous CALLBACK function.the COM library is initialized by CoInitialize(NULL).
Then when the thread uses the COM object's function an error occurred:
Method call IOPCAsyncIO2::Write failed with error code 8001010e
I use Error Lookup for help:the application is calling an Interface which is organized by another thread
the thread is supposed to be the COM thread.
any ideas?
sorry for not giving you the specific codes few days ago. This time I want to describe my issue using the pseudo code:
first:
in class A,func1
r1 = CoInitialize(NULL);
then something for judgement
in class B(this class is created as a winthread and used for accessing the data from callback class take CallbackClass as example).
in class A,I initialize the thread by calling
m_pThread = AfxBeginThread(RUNTIME_CLASS(CTestThread), THREAD_PRIORITY_NORMAL,0,0,NULL);
calling the functions of Class B by
m_pThread->PostThreadMessage(WM_INITIALIZETHREAD, (WPARAM)this, 0);
one can be aware that the Class A is set for just coding and building the relationship between the dialog, Class B and the CallbackClass.
My question is: I have read many blogs about the MTA and STA, and I know my PC's HKEY_CLASSES_ROOT says the ThreadingModel is Apartment. When I use the Class B(Thread class) to get the pointer of the CallbackClass,there the issue happens.
I know there must be something I missed about my thread and I need some example of at least one COM initialization and one win32 thread and how the thread can get access COM's data.Thank you very much.
So, you're sending a "raw" COM interface ('this') to code in another thread. That's probably why you get the error.
COM interface pointers must be passed using COM methods (as arguments to the method so COM knows it has to marshal it to another thread/apartment), not by other means. When in apartment model, you think of it like every COM object lives in each own process (you can't use 'this' in another process, and that's the same rule for apartments).
You could use "COM Connection points" (see here + google for some explanations: An introduction to COM connection points) which are like events between COM objects. Note this can be complicated.
You could also use more low-level constructs and marshal interface pointers yourself (using the CoMarshalInterThreadInterfaceInStream function). See another article on this here: What are the rules for CoMarshalInterThreadInterfaceInStream and CoGetInterfaceAndReleaseStream?. I would try that first.