-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Can't enqueue instance methods #582
Comments
@EliFinkelshteyn , This is how it works. q.enqueue(MyClass.MyClassMethod,Params) # MyClass.
class MyClass(object):
# Initializer.
def __init__(self):
self.MySQL = MySQL
@classmethod
def MyClassMethod(self,Params):
Class = self
self = Class()
# do something here. |
@JohnSundarraj, thanks for the response! I'm not sure how that answers the question. Here's what I currently have: class MyClass:
def __init__(self, config_path):
self.mysql_connector = MySQLConnector(config_path)
def do_db_thing(self, some_data):
sql_sess = self.mysql_connector.get_mysql_connection()
# do something with sql_sess and some_data here Using a |
@EliFinkelshteyn , without creating an instance you cannot use the My personal solution for this, is to make db connection class as singleton, so that we don't create new instance everytime. from sqlalchemy.engine import create_engine
class MySQL(object):
_Instances = dict()
def __new__(self):
if 'Instance' in MySQL._Instances:
return MySQL._Instances['Instance']
else:
self.Engine = create_engine(
'mysql+mysqldb://User:Password@Host:3306/dbname?charset=utf8&use_unicode=0',
pool_size=10
).connect()
return super(MySQL,self).__new__(self)
def __init__(self):
MySQL._Instances['Instance'] = self |
We use Python's |
@EliFinkelshteyn I am interested to know how you ended up doing. I am in the same case, and not really eager to rewrite my class. |
I'm sort of in the situation, the difference is that I can't get rq to pickle a (static) method with @staticmethod annotation. Any suggestions? |
For those looking for a serialization workaround, you can control serializaton and deserialization with Python's Example: https://docs.python.org/3/library/pickle.html#pickle-state I've used this approach with the multiprocessing module in the past. I just tested it with RQ pickling an instance method whose instances has a database connection and it seems to work. I haven't used static methods, but googling for picklable static methods comes up with a good lead: https://stackoverflow.com/questions/1914261/pickling-a-staticmethod-in-python |
Class Nike():
@job
task_async.delay(arg):
#do stuff
def do_it(self, arg):
self.task_async.delay(arg) i get this error:
How i instantiate it and call it n = Nike.do_it(args) |
This is the same issue as #189, which as far as I can tell was never solved. I need to enqueue a class method instead of just a regular function, and I get:
Is there a recommended way around this?
I really need access to a SQLAlchemy session in the function I want to enqueue, but I can't even work around it by making the function accept that separately:
The text was updated successfully, but these errors were encountered: