所以在那些过去的日子里,我一直试图弄清楚如何开始获得线程,我终于让它工作了!我现在遇到的问题是我希望它们同时运行。我听说像GIL这样不同的事情不会起作用。有人说它适用于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并继续这样做,我的愿望是将它同时/同时转换为可能的话?
修改
CODE
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
答案 0 :(得分:2)
我认为您需要修复args参数:
...
jobs = []
for i, e in enumerate(config):
c = (config[i] or e)
p = multiprocessing.Process(target=start, args=(c,))
jobs.append(p)
p.start()
请注意args
参数中使用的语法:
args=(c,)) # <=== (c,) means that we are assigning the tuple (c,) to args
# which gets translated into start(c)