Guest User

Untitled

a guest
Nov 19th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. from multiprocessing import Process
  2. import paramiko
  3. import logging
  4. import sys
  5.  
  6.  
  7. def sftp_read():
  8. # log.debug("Child process started") # This line will cause exception if it is run in sub process.
  9. ssh = paramiko.SSHClient()
  10. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11. timeout = 60
  12. ssh.connect('my_server', username='my_user', password='my_password', timeout=timeout, auth_timeout=timeout,
  13. banner_timeout=timeout)
  14. sftp = ssh.open_sftp()
  15. fp = sftp.file('/home/my_user/my_file.txt')
  16. lines = fp.readlines()
  17. print ''.join(lines)
  18. fp.close()
  19. ssh.close()
  20.  
  21.  
  22. def main():
  23. sftp_read() # Call this function without multiprocessing
  24.  
  25. if __name__ == '__main__':
  26. logging.basicConfig(stream=sys.stdout,
  27. format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s')
  28. log = logging.getLogger()
  29. log.setLevel(logging.DEBUG)
  30. main()
  31.  
  32. [2018-11-20 10:38:45,051] {transport.py:1746} DEBUG - starting thread (client mode): 0x3052208L
  33. [2018-11-20 10:38:45,051] {transport.py:1746} DEBUG - Local version/idstring: SSH-2.0-paramiko_2.4.2
  34. [2018-11-20 10:38:45,405] {transport.py:1746} DEBUG - Remote version/idstring: SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.6
  35. [2018-11-20 10:38:45,405] {transport.py:1746} INFO - Connected (version 2.0, client OpenSSH_7.2p2)
  36.  
  37. def main():
  38. # Use multiprocessing to limit the running time to at most 15 seconds.
  39. p = Process(target=sftp_read)
  40. try:
  41. log.debug("About to start SSH")
  42. p.start()
  43. log.debug('Process started')
  44. p.join(15)
  45. finally:
  46. if p.is_alive():
  47. p.terminate()
  48. log.debug('Terminated')
  49. else:
  50. log.debug("Finished normally")
  51.  
  52. import logging
  53. from multiprocessing import Pool
  54. from os import getpid
  55.  
  56. def runs_in_subprocess():
  57. logging.info(
  58. "I am the child, with PID {}".format(getpid()))
  59.  
  60. if __name__ == '__main__':
  61. logging.basicConfig(
  62. format='GADZOOKS %(message)s', level=logging.DEBUG)
  63.  
  64. logging.info(
  65. "I am the parent, with PID {}".format(getpid()))
  66.  
  67. with Pool() as pool:
  68. pool.apply(runs_in_subprocess)
  69.  
  70. GADZOOKS I am the parent, with PID 3884
  71. GADZOOKS I am the child, with PID 3885
  72.  
  73. from multiprocessing import get_context
  74.  
  75. def your_func():
  76. with get_context("spawn").Pool() as pool:
  77. # ... everything else is unchanged
Add Comment
Please, Sign In to add comment