I have a program that writes files to a network share at a high rate, from a few (3) threads at once.
After running for a while (usually a short while) some of these threads get stuck. Using Process Monitor, I can see that there are calls to WriteFile and CloseFile that simply have no answer.
At this point, I can't shut down the process at all, even killing it from the task manager does nothing.
The interesting thing is that this happens when the computer hosting the shares is running Windows Server 2008 (R2). If I move the shares to a Windows 2003 computer, I don't see these problems. Also, I only see this problem if the program is run on a computer that is running Windows Server 2008 (different computer than the share host).
Here is a short program that quickly reproduces the problem. The files in the source directory range in size from 1 to 20 MB:
Imports System.IO
Imports System.Threading
Module Module1
Private m_sourceFiles As FileInfo()
Private m_targetDir As String
Sub Main(ByVal args As String())
Dim sourceDir As New DirectoryInfo(args(0))
m_sourceFiles = sourceDir.GetFiles()
m_targetDir = args(1)
For i As Integer = 0 To 2
ThreadPool.QueueUserWorkItem(AddressOf DoWork)
Next
Console.ReadLine()
End Sub
Private Const BUFFER_SIZE As Integer = (128 * 1024)
Private Sub DoWork(ByVal o As Object)
Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
Dim random As New Random(Thread.CurrentThread.ManagedThreadId)
While True
Dim fileIndex As Integer = random.Next(m_sourceFiles.Count)
Dim sourceFile As FileInfo = m_sourceFiles(fileIndex)
Dim input As FileStream = sourceFile.OpenRead
Dim targetName As String = sourceFile.Name.Replace(sourceFile.Extension, random.Next(Integer.MaxValue) & sourceFile.Extension)
Dim targetPath As String = m_targetDir & "\" & targetName
Dim output As FileStream = File.Create(targetPath)
Dim bytes() As Byte = New Byte((BUFFER_SIZE) - 1) {}
Dim read As Integer = input.Read(bytes, 0, bytes.Length)
While read <> 0
output.Write(bytes, 0, read)
read = input.Read(bytes, 0, bytes.Length)
End While
output.Flush()
output.Close()
Console.WriteLine(Thread.CurrentThread.ManagedThreadId & " - " & targetName)
End While
End Sub
End Module
The problem was caused by Symantec Antivirus.
Apparently they don't support 2008 R1 yet.
I was able to workaround the issue by disabling SMB 2.0 on the client computer, as described here:
sc config lanmanworkstation depend= bowser/mrxsmb10/nsi
sc config mrxsmb20 start= disabled
Related
i'm not a expert in python and it's my fist post in stack overflow.
Maybe you can help me
I try to use python instead of vba to create document for a specific application.
This application is used to configure Programmable RTUs
It is possible to automate the creation of documents via excel by activating in excel a reference to this specific application.
This is a exemple to add a specific tag to a exsisting timer in a document with VBA in Excel
Sub timer()
Dim TWsdoc As TWinSoft.Document
Dim MyTag As TWinSoft.Tagname
Dim file_path As String
file_path = ThisWorkbook.Path
Set TWsdoc = GetObject("/RTU:MS32S2", "TWinSoft.Document")
Set TWsdoc = GetObject(file_path & "\test.tws")
Dim MyTimer As TWinSoft.OSCTimer
Set MyTimer = TWsdoc.OSCTimers("Timer_1")
'add Status Tag in Timer_1
Set MyTimer.Status = TWsdoc.Tagnames("Timer_1_State")
TWsdoc.Save 'save the document
Set TWsdoc = Nothing
End Sub
This is the python code i use to:
Creat a tag (status) with soms parameter
creat a timer (Timer 1)
try to associate timer status to a timer
and save the document
I use Visual Studio Code
THE problem, i can not associate the tag Timer_1_Status to the Timer_1 like in vba
I always have this error
Une exception s'est produite : AttributeError Property
'.Status' can not be set.
from multiprocessing import context
import win32com.client #need pywin32 (pip install pywin32)
import os, os.path
file = "\\test.tws"
file_path = os.path.dirname(os.path.abspath(__file__))
file_path = file_path + file
twdoc = win32com.client.Dispatch('Twinsoft.Document')
#-----------Creat Timer Status Tag-----------------
tagTimer1Sta=twdoc.AddTag('Timer_1_Status','DIV')
tagTimer1Sta.comment = 'Timer 1 Status'
tagTimer1Sta.ModbusAddress = 5000
#-----------Creat Timer-----------------
twdoc.AddOSCTimer("Timer_1")
#----------Associate Timer Status Tag to Timer
mytimer = twdoc.OSCTimers("Timer_1")
mytimer.Status = twdoc.Tagnames(tagTimer1Sta)
#-----------Save the created tws Document-----------------------
twdoc.SaveAs(file_path)
I thank you in advance for your help and I apologize for the bad English that I write, I am French.....
Yvan
I have written the following VBA code to automate SAS processes.
Rem Start the SAS server
Dim SASws As SAS.Workspace
Dim SASwsm As New SASWorkspaceManager.WorkspaceManager
Dim strError As String
Set SASws = SASwsm.Workspaces.CreateWorkspaceByServer _
("MySAS", VisibilityProcess, Nothing, "", "", strError)
Dim code_location As String, code_name As String, param_str As String
Dim param_flag As Boolean
code_location = "file:" & ThisWorkbook.Sheets("Control").Range("B2").Value
code_name = ThisWorkbook.Sheets("Control").Range("C2").Value
param_flag = ThisWorkbook.Sheets("Control").Range("C4").Value
If param_flag = True Then
param_str = ThisWorkbook.Sheets("Control").Range("D5").Value
Else: param_str = "ds=Sasuser.Export_output"
End If
Rem Run the stored process
Dim SASproc As SAS.StoredProcessService
Set SASproc = SASws.LanguageService.StoredProcessService
SASproc.Repository = code_location
SASproc.Execute code_name, "ds=Sasuser.Export_output"
'SASproc.Repository = "file:C:\Duopa_Repository\SAS_Codes\Weekly"
'SASproc.Execute "weekly_refresh_run.sas", "ds=Sasuser.Export_output"
Rem Shut down the SAS server
SASwsm.Workspaces.RemoveWorkspaceByUUID SASws.UniqueIdentifier
SASws.Close
Set SASws = Nothing
All the SAS codes seem to work through this except the ones that import csv files using proc import method. Can someone please explain me why
Thanks in advance
I am creating a portscanner (via Python) and whenever I receive something, it should be written in an .XLSV file.
What it should do:
Webscanner finds port 21 open
Receives data
Writes it in a .XLSV file in row 2
Webscanner finds port 80 open
Receives data
Writes it in a .XLSV file in row 3
My code:
wb = load_workbook('scanreport.xlsx')
hitdetails = (str(hostname), str(host), str(port), str(keyword), str(banner))
wb = Workbook()
ws = wb.active
start_row = 1
start_column = 1
for searchresult in hitdetails:
ws.cell(row=start_row, column=start_column).value = searchresult
start_column += 1
start_row += 1
wb.save("scanreport.xlsx")
Result:
How can I fix this?
#skjoshi, you sir just fixed my problem with overwriting:
write into excel file without overwriting old content with openpyxl (this page)
Because I am already loading an existing file with an existing sheet, I was also creating a new worksheet, which will overwrite the old one in which I lose my data everytime.
In this case I removed the ws = wb.active and it worked!
Currently I am working in Excel vba. I dont know much about it.
I am doing file transfer operation using putty sftp command,
I am using below code for the same.
I have also installed winscp & putty on my pc.
My code is:
Dim shell As Object
Dim command As String
Dim errorCode As Integer
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 0
Set shell = VBA.CreateObject("WScript.Shell")
command = "cmd.exe /c echo y |"C:\Program Files (x86)\PuTTY\pscp.exe"
-sftp -P 22 -l Username_Here -pw Password_Here
SRC_IP_Address_Here:File_Path_Here " " Destination_Path_Here
' error code is 0 if all ok otherwise 1
errorCode = shell.Run(command, windowStyle, waitOnReturn)
Above code works fine if ip address & destination path exists,
but if ip address is not reachable in network excel takes 20-30 seconds for response & errorCode = 1.
Is it possible to modify above code in such a way that it should give me response only within 10 sec?
How can I set putty sftp connection timeout in above code?
I tried following links but not getting any result:
https://patrickmn.com/aside/how-to-keep-alive-ssh-sessions/
https://www.computerhope.com/unix/sftp.htm
https://library.netapp.com/ecmdocs/ECMP1196993/html/GUID-02FF883B-9913-4137-BC8B-5CB47282B944.html
https://tartarus.org/~simon/putty-snapshots/htmldoc/Chapter5.html
I have more 10000 files in a folder (extension *.WIR - plain text inside though). I want to find and replace specific line inside every single one. My problem is this line is slightly different in every file + I don't know how to force script to batch process.
Example file 1 have: "INSULATION RESIS 20.0 M ohm"
Example file 2 have: "INSULATION RESIS 100.0 M ohm"
Example file 3 have: "INSULATION RESIS 0.2 M ohm"
...
I need to change them all to "INSULATION RESIS 500.0 M ohm".
Is there any chance VBS script can use wildcards to batch process everything in the same time. How can I do that?
Many thanks in advance for any response.
Three strategies for three (sub)problems:
cscript 01.vbs
xxA 1.0 BxxA 1.1 BxxA 123.456 Bxx
xxA 500.0 BxxA 500.0 BxxA 500.0 Bxx
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
' use RegExp to replace all occurrences of "A #.# B" in a string
Dim reRpl : Set reRpl = New RegExp
Dim sTest : sTest = "xxA 1.0 BxxA 1.1 BxxA 123.456 Bxx"
reRpl.Global = True
reRpl.Pattern = "(A )\d+\.\d+( B)"
WScript.Echo sTest
WScript.Echo reRpl.Replace(sTest, "$1500.0$2")
' use FileSystemObject/Stream to read/write from/to file
Dim sAll : sAll = oFS.OpenTextFile(WScript.ScriptFullName).ReadAll()
WScript.Echo sAll
oFS.CreateTextFile(WScript.ScriptFullName, True).Write sAll
' use FileSystemObject/Folder/File to loop directory
Dim oFile
For Each oFile In oFS.GetFolder(".").Files
WScript.Echo oFile.Name
Next
username24.txt
01.vbs
00.vbs
I would probably do something like this:
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "(INSULATION RESIS) \d+\.\d+ (M ohm)"
For Each f In fso.GetFolder("...").Files
If LCase(fso.GetExtensionName(f)) = "wir" Then
filename = f.Path
tempname = filename & ".tmp"
Set f_in = fso.OpenTextFile(filename)
Set f_out = fso.OpenTextFile(tempname, 2)
Do Until f_in.AtEndOfStream
f_out.WriteLine re.Replace(f_in.ReadLine, "$1 500.0 $2")
Loop
f_in.Close
f_out.Close
f.Delete
fso.MoveFile tempname, filename
End If
Next
This solution avoids potential memory exhaustion due to large input files by processing each file line-by-line. Disadvantage of this solution is that the output-handling requires additional steps (write output to temporary file, replace input file with temporary file).