所以在过去的几天里,我一直在试图找出如何开始使用线程,我终于找到了它的工作!我现在面临的问题是,我希望它们同时运行。我听到了很多不同的事情,比如吉尔不行。有人说它与
multiprocessing
等等。。然而,我转过身来,看看这是否可能,有什么发言权这样做?
基本上,我的代码现在看起来是这样的:
def start(config):
NameUrl = config["Url"]
myNote = config["My-Note"]
checkoutNames(Nameurl, MyNote)
if __name__ == '__main__':
with open('config.json', 'r', encoding='UTF-8') as json_data:
config = json.load(json_data)
threads = []
for i, e in enumerate(config):
threads.append(threading.Thread(target=start, args=(config[i] or e)))
正如你所见
if __name__ == '__main__':
这就是此时的线索所在。然而,这在此时所做的是,它首先处理线程1,当它完成时,它会变成线程2,然后继续这样,如果可能的话,我的希望是将它同时转化为?
编辑
代码
if __name__ == '__main__':
with open('config.json', 'r', encoding='UTF-8') as json_data:
config = json.load(json_data)
jobs = []
for i, e in enumerate(config):
c = (config[i] or e)
p = multiprocessing.Process(target=start, args=(c))
jobs.append(p)
p.start()
[<Process(Process-1, initial)>]
<Process(Process-1, initial)>
{'Email': '123o@gmail.com', 'PersonNumber': '4234', 'ZipCode': '1241234', 'Name': 'Guess', 'LastName': 'TheyKnow'}
[<Process(Process-1, started)>, <Process(Process-2, initial)>]
<Process(Process-2, initial)>
{'Email': 'Hello@hotmail.com', 'PersonNumber': '1234', 'ZipCode': '56431', 'Name': 'Stack', 'LastName': 'Overflow'}
Process Process-1:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 249, in _bootstrap
self.run()
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
TypeError: start() takes 1 positional argument but 16 were given
Process Process-2:
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 249, in _bootstrap
self.run()
File "C:\Users\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
TypeError: start() takes 1 positional argument but 16 were given