Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Process
- import paramiko
- import logging
- import sys
- def sftp_read():
- # log.debug("Child process started") # This line will cause exception if it is run in sub process.
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- timeout = 60
- ssh.connect('my_server', username='my_user', password='my_password', timeout=timeout, auth_timeout=timeout,
- banner_timeout=timeout)
- sftp = ssh.open_sftp()
- fp = sftp.file('/home/my_user/my_file.txt')
- lines = fp.readlines()
- print ''.join(lines)
- fp.close()
- ssh.close()
- def main():
- sftp_read() # Call this function without multiprocessing
- if __name__ == '__main__':
- logging.basicConfig(stream=sys.stdout,
- format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
- log = logging.getLogger()
- log.setLevel(logging.DEBUG)
- main()
- [2018-11-20 10:38:45,051] {transport.py:1746} DEBUG - starting thread (client mode): 0x3052208L
- [2018-11-20 10:38:45,051] {transport.py:1746} DEBUG - Local version/idstring: SSH-2.0-paramiko_2.4.2
- [2018-11-20 10:38:45,405] {transport.py:1746} DEBUG - Remote version/idstring: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.6
- [2018-11-20 10:38:45,405] {transport.py:1746} INFO - Connected (version 2.0, client OpenSSH_7.2p2)
- def main():
- # Use multiprocessing to limit the running time to at most 15 seconds.
- p = Process(target=sftp_read)
- try:
- log.debug("About to start SSH")
- p.start()
- log.debug('Process started')
- p.join(15)
- finally:
- if p.is_alive():
- p.terminate()
- log.debug('Terminated')
- else:
- log.debug("Finished normally")
- import logging
- from multiprocessing import Pool
- from os import getpid
- def runs_in_subprocess():
- logging.info(
- "I am the child, with PID {}".format(getpid()))
- if __name__ == '__main__':
- logging.basicConfig(
- format='GADZOOKS %(message)s', level=logging.DEBUG)
- logging.info(
- "I am the parent, with PID {}".format(getpid()))
- with Pool() as pool:
- pool.apply(runs_in_subprocess)
- GADZOOKS I am the parent, with PID 3884
- GADZOOKS I am the child, with PID 3885
- from multiprocessing import get_context
- def your_func():
- with get_context("spawn").Pool() as pool:
- # ... everything else is unchanged
Add Comment
Please, Sign In to add comment