这就奇怪了,当我们启动一个新的多进程时,难道不能使用同一个类的另一个方法吗?
我的目标是每次在对象中实例化类时创建一个新进程,我不知道我是否以正确的方式这样做。
我使用 IQ Option 的 API,但它只允许连接一个用户,在多次尝试中,我发现通过多进程,我可以将多个帐户连接到该 API。
但是,当在 __init__() 中创建新进程时,我无法使用该类的其他方法,因为我收到消息 self.API 不是我的类的属性,我将留下下面的示例代码以及调试器发送的确切错误,非常感谢,上帝保佑你们所有人。
from time import sleep
from iqoptionapi.stable_api import IQ_Option
import multiprocessing
class Instance_New_User:
def __init__(self,email,password):
self.email = email
self.password = password
multiprocessing.Process(target=self.connect).start()
def connect(self):
self.API = IQ_Option(self.email,self.password)
self.API.connect()
if self.API.check_connect() == False:
print('Failed to connect')
if self.API.check_connect() == True:
print('Connected')
def get_balance(self):
return self.API.get_balance()
if __name__ == '__main__':
email = str(input('Put here your email of IQ Option:'))
password = str(input('Put here your password of IQ Option:'))
var = Instance_New_User(email,password)
print(var.get_balance()) <- I want to use a method of a class
ERROR : AttributeError: 'Instance_New_User' object has no attribute 'API'
我的目标是每次在对象中实例化类时创建一个新进程,我不知道我是否以正确的方式这样做。
My goal is to create a new process every time the class is instantiated in an object, I don't know if I'm doing it the right way.
I use an API from IQ Option, but it only allows the connection of only one user, among several attempts, I discovered that with Multi-process, I can connect more than one account to the API.
But when creating a new process in __init__()
I can't use the other methods of the class, because I get the message that the self.API is not an attribute of my class, I'll be leaving the example code below and also the exact error that the debugger sent, thank you very much and god bless you all.
from time import sleep
from iqoptionapi.stable_api import IQ_Option
import multiprocessing
class Instance_New_User:
def __init__(self,email,password):
self.email = email
self.password = password
multiprocessing.Process(target=self.connect).start()
def connect(self):
self.API = IQ_Option(self.email,self.password)
self.API.connect()
if self.API.check_connect() == False:
print('Failed to connect')
if self.API.check_connect() == True:
print('Connected')
def get_balance(self):
return self.API.get_balance()
if __name__ == '__main__':
email = str(input('Put here your email of IQ Option:'))
password = str(input('Put here your password of IQ Option:'))
var = Instance_New_User(email,password)
print(var.get_balance()) <- I want to use a method of a class
ERROR : AttributeError: 'Instance_New_User' object has no attribute 'API'
My goal is to create a new process every time the class is instantiated in an object, I don't know if I'm doing it the right way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您以错误的方式使用多处理。
当您使用
多处理。程序(....)。开始()
时,您实际上是在使用新的地址空间开始新的过程。该过程获取您用于启动它的对象的。因此,您的主线程不会在子线程中看到您对self
的任何更改,反之亦然。因此,self.api
不存在。要与线程交谈,您需要设置某种通信渠道。您会将消息发送到“请告诉我余额”的线程,线程将呼叫self.api .....并将结果报告回去。除非询问子过程,否则主线程无法访问该连接。
You are using multiprocessing the wrong way.
When you use
multiprocessing.Process(....).start()
, you are literally starting a new process with a new address space. That process gets copies of the objects that you use to start it. So your main thread doesn't see any changes you make toself
in the sub-thread, and vice versa. Henceself.API
doesn't exist.To talk to a thread, you need to set up some sort of communication channel. You would send a message to the thread "Please tell me the balance", and the thread, would call self.API..... and report the result back. You main thread has no access to the connection except by asking the subprocess.