MultiProcessing Python not working in windows - python

Traceback (most recent call last): File
"C:\Users\HP\Downloads\1\main.py", line 27, in <module>
p.start() File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py",
line 121, in start
self._popen = self._Popen(self) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj) File
"C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
line 326, in _Popen
return Popen(process_obj) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py",
line 93, in _init_
reduction.dump(process_obj, to_child) File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py",
line 60, in dump
ForkingPickler(file, protocol).dump(obj) pickle.PicklingError: Can't pickle <function <lambda> at 0x039E9538>: attribute lookup
<lambda> on __main_ failed
C:\Users\HP\Downloads\1>Traceback (most recent call last): File
"<string>", line 1, in <module> File
"C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py",
line 102, in spawn_main
source_process = _winapi.OpenProcess( OSError: [WinError 87] The parameter is incorrect
""
Im trying to open multiple chromedrivers/files.py at the same time/instant.
But it shows the error.
The code is to get the sample from file.py and create multiple same files like file_1.py.....
Then run all the files that has created but is not working.
The Code is:
for proxy in file:
shutil.copyfile('/home/hp/Documents/pr/proxy_project/template.py', '/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i))
file_name = "file_{}".format(i)
file_list.append(file_name)
with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'r') as replicated_file:
data = replicated_file.readlines()
data_to_be_added = "proxy = '{}'\n".format(proxy.strip("\n"))
data[16] = data_to_be_added
with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'w') as new_replicated_file:
new_replicated_file.writelines(data)
i += 1
for file_py in tuple(file_list):
print("File name = {}".format(file_py))
p = multiprocessing.Process(target=lambda: __import__(file_py))
p.start()
There is another file that will be replicated when this code runs

On Windows when using multiprocessing you have to protect your main code with if name==‘main’: - try reading the multiprocessing documentation. – barny Mar 23 at 19:46

Related

get_coherence() function error in python while using LDA

I have a problem with using coherence model
my code is
def compute_coherence_values(dictionary, corpus, texts, limit, start, step):
coherence_values = []
model_list = []
for num_topics in range(start, limit, step):
model = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=id2word, num_topics=num_topics)
model_list.append(model)
coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence="c_v")
coherence_values.append(coherencemodel.get_coherence())
return model_list, coherence_values
coherence_values = []
model_list = []
# topic number
nt = pre_nt
start_ = nt;
limit_ = nt + 1;
step_ = 1;
model_list1, coherence_values1 = compute_coherence_values(dictionary=id2word, corpus=corpus, texts=texts_wi_new,
start=start_, limit=limit_, step=step_)
and the error is
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\spawn.py", line 105, in spawn_main
Traceback (most recent call last):
File "<input>", line 3, in <module>
File "<input>", line 92, in compute_coherence_values
File "D:\All Python\venv\lib\site-packages\gensim\models\coherencemodel.py", line 609, in get_coherence
confirmed_measures = self.get_coherence_per_topic()
File "D:\All Python\venv\lib\site-packages\gensim\models\coherencemodel.py", line 569, in get_coherence_per_topic
self.estimate_probabilities(segmented_topics)
File "D:\All Python\venv\lib\site-packages\gensim\models\coherencemodel.py", line 541, in estimate_probabilities
self._accumulator = self.measure.prob(**kwargs)
File "D:\All Python\venv\lib\site-packages\gensim\topic_coherence\probability_estimation.py", line 156, in p_boolean_sliding_window
return accumulator.accumulate(texts, window_size)
File "D:\All Python\venv\lib\site-packages\gensim\topic_coherence\text_analysis.py", line 444, in accumulate
workers, input_q, output_q = self.start_workers(window_size)
File "D:\All Python\venv\lib\site-packages\gensim\topic_coherence\text_analysis.py", line 478, in start_workers
worker.start()
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
BrokenPipeError: [Errno 32] Broken pipe
exitcode = _main(fd)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\spawn.py", line 114, in _main
prepare(preparation_data)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\spawn.py", line 225, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
run_name="__mp_main__")
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\runpy.py", line 261, in run_path
code, fname = _get_code_from_file(run_name, path_name)
File "C:\Users\lee96\AppData\Local\Programs\Python\Python37\Lib\runpy.py", line 231, in _get_code_from_file
with open(fname, "rb") as f:
OSError: [Errno 22] Invalid argument: 'D:\\All Python\\<input>'
The error occurs in this part
coherencemodel.get_coherence()
I use pycharm.
How can I solve it?
sorry
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
It looks like your post is mostly code; please add some more details.
I'm having the same exact issue with the same exact code. The code works perfectly fine when I run it from my Spyder IDE, but when I plug it into Power BI, it errors out. So far, I've broken it out of the function and loop into the basic lines below. The LDA and Coherence model runs fine, but for some reason when get_coherence() is called it errors out.
model = gensim.models.ldamodel.LdaModel(corpus, num_topics=5, id2word=dictionary, passes=10)
coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
test = coherencemodel.get_coherence()
Below is part of the error message I received back:
RuntimeError: An attempt has been made to start a new process before
the current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your child
processes and you have forgotten to use the proper idiom in the main
module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program is not going
to be frozen to produce an executable.
Details:
DataSourceKind=Python
DataSourcePath=Python
Message=Python script error.
I did some more researching on this and found a few other articles that were helpful for me, but ultimately it seems like the errors have to do with multiprocessing within a windows framework.
where to put freeze_support() in a Python script?
https://docs.python.org/2/library/multiprocessing.html#windows
What worked for me, is that I placed all of my code under the following line of code:
if __name__ == '__main__':
freeze_support()
model_list, coherence_values = compute_coherence_values(dictionary=dictionary, corpus=corpus, texts=texts, start=start, limit=limit, step=step)
max_value = max(coherence_values)
max_index = coherence_values.index(max_value)
best_model = model_list[max_index]
ldamodel= best_model
I'm not the greatest developer within Python, but I got it working for what I needed. If others have better suggestions, I'm all eyes and ears :)

How to run decorated function in a separate and terminatable process?

I am dealing with an existing test suite, where we want to implement a timeout functionality, which will cause a hanging test to time out and then move on with its regular teardown/cleanup.
I am toying with the idea of running each test in a process, which I can terminate after e.g. a timeout of 3 seconds. Ideally, I don't want to modify the test cases and instead just add a decorator indicating the test is affected by this timeout behavior.
This is what I have, a minimal example:
import multiprocessing
import sys
from time import sleep
def timeout(func):
def wrapper():
proc = multiprocessing.Process(target=func)
proc.start()
sleep(3)
proc.terminate()
return wrapper
#timeout
def my_test():
while True:
sleep(1)
if __name__ == "__main__":
my_test()
But for some reason, it seems pickle cannot deal with this and the decorator somehow messes up the reference to the function, as this error is hit:
$ python multiproc.py
2019-11-07 07:34:37.098 | DEBUG | __main__:wrapper:13 - In wrapper
Traceback (most recent call last):
File "multiproc.py", line 30, in <module>
my_test()
File "multiproc.py", line 15, in wrapper
proc.start()
File "C:\Python36\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Python36\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Python36\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Python36\lib\multiprocessing\popen_spawn_win32.py", line 65, in __init__
reduction.dump(process_obj, to_child)
File "C:\Python36\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function my_test at 0x000001C0A7E87400>: it's not the same object as __main__.my_test
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python36\lib\multiprocessing\spawn.py", line 99, in spawn_main
new_handle = reduction.steal_handle(parent_pid, pipe_handle)
File "C:\Python36\lib\multiprocessing\reduction.py", line 82, in steal_handle
_winapi.PROCESS_DUP_HANDLE, False, source_pid)
OSError: [WinError 87] The parameter is incorrect
Does anyone have an idea if this can be solved without modifying the existing test case?

How to run playbook api in Ansible with vault

I have a playbook with vault, and I can run it through:
ansible-playbook info.yml --ask-vault-pass
Now, I want to run my playbook api in Ansible. The answer in How to run playbook api in Ansible v2 with vault said that we can set
loader = DataLoader()
loader.set_vault_password('mypass')
in 2.2.0.0 API, and I also set these in my api,but it doesn't work. The error message is as follows:
Traceback (most recent call last):
File "test2.py", line 63, in <module>
playbook.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in run
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 78, in _load_playbook_data
ds = self._loader.load_from_file(os.path.basename(file_name))
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 130, in load_from_file
(b_file_data, show_content) = self._get_file_contents(file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 206, in _get_file_contents
data = self._vault.decrypt(data, filename=b_file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 561, in decrypt
plaintext, vault_id = self.decrypt_and_get_vault_id(vaulttext, filename=filename)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 617, in decrypt_and_get_vault_id
_matches = match_secrets(self.secrets, vault_id_matchers)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 456, in match_secrets
matches = [(vault_id, secret) for vault_id, secret in secrets if vault_id in target_vault_ids]
ValueError: need more than 1 value to unpack
Then I change the
loader.set_vault_password('mypass')
to
loader.set_vault_secrets([('default','mypass')])
But it doesn't work either. The error message is:
Traceback (most recent call last):
File "test2.py", line 63, in <module>
playbook.run()
File "/usr/lib/python2.7/site-packages/ansible/executor/playbook_executor.py", line 82, in
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._l
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 54, in load
pb._load_playbook_data(file_name=file_name, variable_manager=variable_manager)
File "/usr/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 78, in _load_pla
ds = self._loader.load_from_file(os.path.basename(file_name))
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 130, in load_fr
(b_file_data, show_content) = self._get_file_contents(file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/dataloader.py", line 206, in _get_fi
data = self._vault.decrypt(data, filename=b_file_name)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 561, in dec
plaintext, vault_id = self.decrypt_and_get_vault_id(vaulttext, filename=filename)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 638, in dec
b_plaintext = this_cipher.decrypt(b_vaulttext, vault_secret)
File "/usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py", line 1344, in de
b_password = secret.bytes
AttributeError: 'str' object has no attribute 'bytes'
I try to change the line
b_password = secret.bytes
in file /usr/lib/python2.7/site-packages/ansible/parsing/vault/__init__.py to
b_password = secret
then the api works normally. But I think it's not a good way to basically solve my problem.
Use VaultSecret class instead of string password:
from ansible.module_utils._text import to_bytes
from ansible.parsing.vault import VaultSecret
loader.set_vault_secrets([('default',VaultSecret(_bytes=to_bytes('123456')))])

Python multiprocessing with Process : "could not create trigger pipe"

I'm trying to multiprocess a code I've just written in Python using multiprocessing library.
Here is the part of the code I'm trying to multiprocess:
for b in (Bulle):
p=[]
for i in range(len(X)):
print(i,"/",len(X))
for j in range(len(Y)):
for n in b.voisins:
c_pos=[X[i][j],Y[i][j]]
if __name__ == "__main__":
p.append(Process(target=fonction_courant2,args=(b,n,c_pos)))
for i in (range(len(p))): p[i].start()
for i in (range(len(p))): p[i].join()
Output:
NotifierThreadProc: could not create trigger pipe
Traceback (most recent call last):
File "multi_t_bubbles.py", line 113, in <module>
for i in (range(len(p))): p[i].start()
File "/usr/lib/python3.4/multiprocessing/process.py", line 105, in start
self._popen = self._Popen(self)
File "/usr/lib/python3.4/multiprocessing/context.py", line 212, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "/usr/lib/python3.4/multiprocessing/context.py", line 267, in _Popen
return Popen(process_obj)
File "/usr/lib/python3.4/multiprocessing/popen_fork.py", line 21, in __init__
self._launch(process_obj)
File "/usr/lib/python3.4/multiprocessing/popen_fork.py", line 69, in _launch
parent_r, child_w = os.pipe()
OSError: [Errno 24] Too many open files
I'm fairly new in multiprocessing (I took https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process as a base for this code) and I don't understand this output. It seems that the creation of Processes is ok, but not the start.
I would be really grateful if you have any suggestions!
Tom

Open a new instance of AutoCAD via Python

Does anyone have any experience using Python with AutoCAD? I'm just trying, as a test to see if I can open a new instance of AutoCAD via Python and though that PyAutocad worked well (feel free to offer other suggestions, if you have any).
Anyway based on the doc (https://pypi.python.org/pypi/pyautocad/#downloads) - it says these lines of code should do it, but nothing's happened as of yet.
from pyautocad import Autocad, APoint
acad = Autocad()
acad.prompt("Hello, Autocad from Python\n")
Just these lines of code should generate info on the commandline but instead results in about 50 lines worth of traceback (which I can post as well if anyone's interested) - any ideas?
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
acad.prompt("Hello, Autocad")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 180, in GetActiveObject
obj = comtypes.GetActiveObject(clsid, interface=interface)
File "C:\Python27\lib\site-packages\comtypes\__init__.py", line 1165, in GetActiveObject
oledll.oleaut32.GetActiveObject(byref(clsid), None, byref(p))
File "_ctypes/callproc.c", line 941, in GetResult
WindowsError: [Error -2147221021] Operation unavailable
After adding the VBA Module, the traceback now reads:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
acad.prompt("Hello")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 54, in app
self._app = comtypes.client.GetActiveObject('AutoCAD.Application')
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 183, in GetActiveObject
return _manage(obj, clsid, interface=interface)
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 188, in _manage
obj = GetBestInterface(obj)
File "C:\Python27\lib\site-packages\comtypes\client\__init__.py", line 112, in GetBestInterface
interface = getattr(mod, itf_name)
AttributeError: 'module' object has no attribute 'IAcadApplication'
Now after deleting comtypes' "gen" folder per #reclosedev, Autocad now opens but the string of text still will not appear on the command line - the traceback reads:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
acad.prompt("Hello")
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 153, in prompt
self.doc.Utility.Prompt(u"%s\n" % text)
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 65, in doc
return self.app.ActiveDocument
File "C:\Python27\lib\site-packages\pyautocad\api.py", line 59, in app
self._app.Visible = self._visible
File "C:\Python27\lib\site-packages\comtypes\client\dynamic.py", line 116, in __setattr__
dispid = self._comobj.GetIDsOfNames(name)[0]
File "C:\Python27\lib\site-packages\comtypes\automation.py", line 643, in GetIDsOfNames
self.__com_GetIDsOfNames(riid_null, arr, len(names), lcid, ids)
COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
Have you considered IronPython?
http://through-the-interface.typepad.com/through_the_interface/2009/03/using-ironpython-with-autocad.html
Here's a quick example of how it could be leveraged:
import clr
import System
from System import Type, Activator
acApp = Activator.CreateInstance(Type.GetTypeFromProgID("AutoCAD.Application"))
acApp.Visible = 1
acApp.ActiveDocument.SendCommand("(Princ \"Hello World from Python!\")(Princ)\n")
Obviously the ProgId will correspond to whichever version of AutoCAD was opened last.

Categories

Resources