Multithreading, can't run Process command - python

I am trying to the following code:
#!/usr/bin/python
import multiprocessing
def f(name):
print 'hello', name
if __name__ == '__main__':
p = multiprocessing.Process(target=f, args=('bob',))
p.start()
p.join()
The output I get is :
Traceback (most recent call last):
File "a.py", line 9, in <module>
p = multiprocessing.Process(target=f, args=('bob',))
AttributeError: 'module' object has no attribute 'Process'

You are trying to import multiprocessing from your local directory and not from the python library. The python interpreter first tries to import the module from the present directory. As you have got a file with the name multiprocessing.pyc in your directory, the interpreter is trying to import that. Hence you have got the error. Thus deleting multiprocessing.pyc will help resolve your problem.

Dont give the name of the file as "multiprocessing.py", give any other
thanks,
vybhav

The mistake was naming, my script as 'multiprocessing.py', once it was created. I made an another script with name 'a.py' and both of them were not working. After listing the directories, 'multiprocessing.pyc' was located. I deleted this file, and executed 'a.py' file which executed like gem!
thanks to #Bhargav Rao for highlighting

Don't give the name in the same directory as multiprocess.py, use different name instead.

Related

multiprocessing and image aquisition [duplicate]

I am trying to the following code:
#!/usr/bin/python
import multiprocessing
def f(name):
print 'hello', name
if __name__ == '__main__':
p = multiprocessing.Process(target=f, args=('bob',))
p.start()
p.join()
The output I get is :
Traceback (most recent call last):
File "a.py", line 9, in <module>
p = multiprocessing.Process(target=f, args=('bob',))
AttributeError: 'module' object has no attribute 'Process'
You are trying to import multiprocessing from your local directory and not from the python library. The python interpreter first tries to import the module from the present directory. As you have got a file with the name multiprocessing.pyc in your directory, the interpreter is trying to import that. Hence you have got the error. Thus deleting multiprocessing.pyc will help resolve your problem.
Dont give the name of the file as "multiprocessing.py", give any other
thanks,
vybhav
The mistake was naming, my script as 'multiprocessing.py', once it was created. I made an another script with name 'a.py' and both of them were not working. After listing the directories, 'multiprocessing.pyc' was located. I deleted this file, and executed 'a.py' file which executed like gem!
thanks to #Bhargav Rao for highlighting
Don't give the name in the same directory as multiprocess.py, use different name instead.

Print Data from PLC to Python using pycomm

I am trying to move data (a dint specifically but my example is a BOOL) from the plc to python to be used as a variable to display a picture. The issue is, if I use pycomm, I am getting an error in Windows Powershell. I feel this is a very simple error from a basic python mistake rather than a pycomm issue but I am not informed enough to tell.
SysInfo:
configparser==3.5.0
cpppo==3.9.7
greenery==2.1
ipaddress==1.0.18
pycomm==1.0.8
pyreadline==2.1
pytz==2017.2
python==2.7.13
Code I am using:
from pycomm.ab_comm.clx import Driver as ClxDriver
import logging
if __name__ == '__main__':
c = ClxDriver()
if c.open('IPADDRESSHERE'):
print(c.read_tag(['new_Bool']))
c.close()
Which is just a stripped down version of one of the examples on github https://github.com/ruscito/pycomm
This is the result from running powershell:
PS C:\Users\Tom\Documents\PythonProjects> python pycomm2.py
Traceback (most recent call last):
File "pycomm2.py", line 10, in
print(c.read_tag(['new_Bool']))
File "C:\Python27\lib\site-packages\pycomm\ab_comm\clx.py", line 359, in read_tag
self.logger.warning(self._status)
AttributeError: 'Driver' object has no attribute 'logger'
PS C:\Users\Tom\Documents\PythonProjects>
I have looked for this AttributeError and tried to find a solution, but I think the solutions I have found are over my head. If I have failed to provide some details in order for this question to make sense please let me know.
Edit:
from pycomm.ab_comm.clx import Driver as ClxDriver
import logging
if __name__ == '__main__':
logging.basicConfig(
filename="ClxDriver.log",
format="%(levelname)-10s %(asctime)s %(message)s",
level=logging.DEBUG
)
c = ClxDriver()
if c.open('IPADRESSHERE'):
print(c.read_tag(['new_Bool']))
c.close()
Yields the same attribute error.
PS C:\Users\Tom\Documents\PythonProjects> python pycommtest.py
Traceback (most recent call last):
File "pycommtest.py", line 15, in
print(c.read_tag(['new_Bool']))
File "C:\Python27\lib\site-packages\pycomm\ab_comm\clx.py", line 359, in read_tag
self.logger.warning(self._status)
AttributeError: 'Driver' object has no attribute 'logger'
PS C:\Users\Tom\Documents\PythonProjects>
I was able to read a value, but not with pycomm. Using CPPPO, I was able to continuously update a variable as needed. This may not answer the question of what was wrong with my old code, but it is my work around in case someone from the future has to do the same thing. Credit to user Liverpool_chris and the abyss of Reddit.
https://www.reddit.com/r/PLC/comments/5x3y5z/python_cpppo_library_writing_to_tag_in_plc/
from cpppo.server.enip.get_attribute import proxy_simple
import time
host = "IPHERE"
while True:
x, = proxy_simple(host).read(( "CPID"))
print x
time.sleep(5)

Getting "ImportError: No module named" with parallel python and methods in a package

I'm trying to use parallel python in order to do some distributed benchmarking (essentially, coordinate and run some code on a set of machines from a central server). The code I had was working perfectly fine until I moved the functionality to a separate package. From then on, I keep getting ImportError: No module named some.module.pp_test.
My question is actually two-fold: has anyone ever came across this problem with pp, and if yes, how to solve it? I tried using dill (import dill), but didn't help. Also, is there a good replacement for parallelpython, that doesn't require any additional infrastructure?
The exact error I get is:
RUNNING TEST
Waiting for hosts to finish booting....A fatal error has occured during the function execution
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ppworker.py", line 86, in run
__args = pickle.loads(__sargs)
ImportError: No module named some.module.pp_test
Caught exception in the run phase 'NoneType' object is not iterable
Traceback (most recent call last):
File "test.py", line 5, in <module>
p.ping_pong()
File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 5, in ping_pong
a_test.run()
File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 27, in run
pong, hostname = ping()
TypeError: 'NoneType' object is not iterable
The code is structured this way:
pp-test/
test.py
some/
__init__.py
module/
__init__.py
pp_test.py
The test.py is implemented as:
from some.module.pp_test import MWE
p = MWE()
p.ping_pong()
While pp_test.py is:
class MWE():
def ping_pong(self):
print "RUNNING TEST "
a_test = PPTester()
a_test.run()
import pp
import time
from sys import stdout, exit
class PPTester(object):
def run(self):
try:
ppservers = ('10.10.10.10', )
time.sleep(5)
job_server = pp.Server(0, ppservers=ppservers)
stdout.write("Waiting for hosts to finish booting...")
while len(job_server.get_active_nodes()) - 1 < len(ppservers):
stdout.write(".")
stdout.flush()
time.sleep(1)
ppmodules = ()
pings = [(server, job_server.submit(self.run_pong, modules=ppmodules)) for server in ppservers]
for server, ping in pings:
pong, hostname = ping()
print "Host ", hostname, " is alive!"
print "All servers booted up, starting benchmarks..."
job_server.print_stats()
except Exception as e:
print "Caught exception in the run phase", e
raise
pass
def run_pong(self):
import subprocess
p = subprocess.Popen("hostname", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
p_status = p.wait()
return "pong ", output
dill won't work with pp out of the box, because pp doesn't serialize the python objects -- pp extracts the object's source code (like the inspect module in the standard python library).
To enable pp to use dill (actually dill.source, which is inspect augmented by dill), you have to use a fork of pp called ppft. ppft installs as pp (i.e. imports with import pp), but it has much stronger source inspection, so you can automatically "serialize" most python objects and have ppft track down their dependencies automatically.
Get ppft here: https://github.com/uqfoundation
ppft is also pip installable and python 3.x compatible.

Python: Run Script Under Same Window

I am trying to setup a program where when someone enters a command it will run that command which is a script in a sub folder called "lib".
Here is my code:
import os
while 1:
cmd = input(' >: ')
for file in os.listdir('lib'):
if file.endswith('.py'):
try:
os.system(str(cmd + '.py'))
except FileNotFoundError:
print('Command Not Found.')
I have a file: lib/new_user.py But when I try to run it I get this error:
Traceback (most recent call last):
File "C:/Users/Daniel/Desktop/Wasm/Exec.py", line 8, in <module>
exec(str(cmd + '.py'))
File "<string>", line 1, in <module>
NameError: name 'new_user' is not defined
Does anyone know a way around this? I would prefer if the script would be able to be executed under the same window so it doesn't open a completely new one up to run the code there. This may be a really Noob question but I have not been able to find anything on this.
Thanks,
Daniel Alexander
os.system(os.path.join('lib', cmd + '.py'))
You're invoking new_user.py but it is not in the current directory. You need to construct lib/new_user.py.
(I'm not sure what any of this has to do with windows.)
However, a better approach for executing Python code from Python is making them into modules and using import:
import importlib
cmd_module = importlib.import_module(cmd, 'lib')
cmd_module.execute()
(Assuming you have a function execute defined in lib/new_user.py)

How can python subprocess.Popen see select.poll and then later not? (select 'module' object has no attribute 'poll')

I'm using the (awesome) mrjob library from Yelp to run my python programs in Amazon's Elastic Map Reduce. It depends on subprocess in the standard python library. From my mac running python2.7.2, everything works as expected
However, when I switched to using the exact same code on Ubuntu LTS 11.04 also with python2.7.2, I encountered something strange:
mrjob loads the job, and then attempts to communicate with its child processes using subprocess and generates this error:
File "/usr/local/lib/python2.7/dist-packages/mrjob-0.3.1-py2.7.egg/mrjob/emr.py", line 1212, in _build_steps
steps = self._get_steps()
File "/usr/local/lib/python2.7/dist-packages/mrjob-0.3.1-py2.7.egg/mrjob/runner.py", line 1003, in _get_steps
stdout, stderr = steps_proc.communicate()
File "/usr/lib/python2.7/subprocess.py", line 754, in communicate
return self._communicate(input)
File "/usr/lib/python2.7/subprocess.py", line 1302, in _communicate
stdout, stderr = self._communicate_with_poll(input)
File "/usr/lib/python2.7/subprocess.py", line 1332, in _communicate_with_poll
poller = select.poll()
AttributeError: 'module' object has no attribute 'poll'
This appears to be a problem with subprocess and not mrjob.
I dug into /usr/lib/python2.7/subprocess.py and found that during import it runs:
if mswindows:
... snip ...
else:
import select
_has_poll = hasattr(select, 'poll')
By editing that, I verified that it really does set _has_poll==True. And this is correct; easily verified on the command line.
However, when execution progresses to using Popen._communicate_with_poll somehow the select module has changed! This is generated by printing dir(select) right before it attempts to use select.poll().
['EPOLLERR', 'EPOLLET', 'EPOLLHUP', 'EPOLLIN', 'EPOLLMSG',
'EPOLLONESHOT', 'EPOLLOUT', 'EPOLLPRI', 'EPOLLRDBAND',
'EPOLLRDNORM', 'EPOLLWRBAND', 'EPOLLWRNORM', 'PIPE_BUF',
'POLLERR', 'POLLHUP', 'POLLIN', 'POLLMSG', 'POLLNVAL',
'POLLOUT', 'POLLPRI', 'POLLRDBAND', 'POLLRDNORM',
'POLLWRBAND', 'POLLWRNORM', '__doc__', '__name__',
'__package__', 'error', 'select']
no attribute called 'poll'!?!? How did it go away?
So, I hardcoded _has_poll=False and then mrjob happily continues with its work, runs my job in AWS EMR, with subprocess using communicate_with_select... and I'm stuck with a hand-modified standard library...
Any advice? :-)
I had a similar problem and it turns out that gevent replaces the built-in select module with gevent.select.select which doesn't have a poll method (as it is a blocking method).
However for some reason by default gevent doesn't patch subprocess which uses select.poll.
An easy fix is to replace subprocess with gevent.subprocess:
import gevent.monkey
gevent.monkey.patch_all(subprocess=True)
import sys
import gevent.subprocess
sys.modules['subprocess'] = gevent.subprocess
If you do this before importing the mrjob library, it should work fine.
Sorry for writing a full answer instead of a comment, otherwise I'd lose code indentation.
I cannot help you directly since something seems very strictly tied to your code, but I can help you find out, by relying on the fact that Python modules can be arbitrary object, try something like that:
class FakeModule(dict):
def __init__(self, origmodule):
self._origmodule = origmodule
self.__all__ = dir(origmodule)
def __getattr__(self, attr):
return getattr(self._origmodule, attr)
def __delattr__(self, attr):
if attr == "poll":
raise RuntimeError, "Trying to delete poll!"
self._origmodule.__delattr__(attr)
def replaceSelect():
import sys
import select
fakeselect = FakeModule(select)
sys.modules["select"] = fakeselect
replaceSelect()
import select
del select.poll
and you'll get an output like:
Traceback (most recent call last):
File "domy.py", line 27, in <module>
del select.poll
File "domy.py", line 14, in __delattr__
raise RuntimeError, "Trying to delete poll!"
RuntimeError: Trying to delete poll!
By calling replaceSelect() in your code you should be able to get a traceback of where somebody is deleting poll(), so you can understand why.
I hope my FakeModule implementation is good enough, otherwise you might need to modify it.

Categories